update the codebase to latest go src (time, hash, strconv)
[epoint] / pkg / key / key.go
index 29e92bd..6f2a322 100644 (file)
@@ -16,6 +16,7 @@
 package key
 
 import (
+       "bytes"
        "crypto"
        "crypto/dsa"
        "crypto/openpgp"
@@ -25,6 +26,7 @@ import (
        "fmt"
        "io"
        "math/big"
+       "time"
 )
 
 // TODO: keep denomination only in issuer key?
@@ -46,7 +48,7 @@ func DsaKey(r []byte) *dsa.PrivateKey {
 loop:
        h := sha1.New()
        h.Write(r)
-       r = h.Sum()
+       r = h.Sum(nil)
        x.SetBytes(r)
        // TODO: zero out r and h ?
        if x.Sign() == 0 || x.Cmp(priv.Q) >= 0 {
@@ -70,12 +72,11 @@ func RandomDsaKey() (priv *dsa.PrivateKey, err error) {
 // New returns an openpgp.Entity that contains a fresh DSA private key with a
 // single identity composed of the given full name, comment and email, any of
 // which may be empty but must not contain any of "()<>\x00".
-func New(priv *dsa.PrivateKey, currentTimeSecs int64, name, comment, email string) (e *openpgp.Entity, err error) {
+func New(priv *dsa.PrivateKey, t time.Time, name, comment, email string) (e *openpgp.Entity, err error) {
        uid := packet.NewUserId(name, comment, email)
        if uid == nil {
                return nil, fmt.Errorf("NewEntity: invalid argument: user id field contained invalid characters")
        }
-       t := uint32(currentTimeSecs)
        e = &openpgp.Entity{
                PrimaryKey: packet.NewDSAPublicKey(t, &priv.PublicKey, false /* not a subkey */ ),
                PrivateKey: packet.NewDSAPrivateKey(t, priv, false /* not a subkey */ ),
@@ -100,13 +101,32 @@ func New(priv *dsa.PrivateKey, currentTimeSecs int64, name, comment, email strin
        return
 }
 
+// Parse armored or binary openpgp public or private key
+func Parse(d []byte) (e *openpgp.Entity, err error) {
+       elist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(d))
+       if err != nil {
+               elist1, err1 := openpgp.ReadKeyRing(bytes.NewBuffer(d))
+               if err1 != nil {
+                       return
+               }
+               err = nil
+               elist = elist1
+       }
+       if len(elist) != 1 {
+               err = fmt.Errorf("Parse: expected exactly one key")
+               return
+       }
+       e = elist[0]
+       return
+}
+
 // Issuer generates a key for obligation issuer clients from random seed r
 func Issuer(r []byte, denomination string) (e *openpgp.Entity, err error) {
-       return New(DsaKey(r), 0, "Issuer", denomination, "")
+       return New(DsaKey(r), time.Unix(0,0), "Issuer", denomination, "")
 }
 // Holder generates a key for obligation holder clients from random seed r
 func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error) {
-       return New(DsaKey(r), 0, "Holder of "+issuer, denomination, "")
+       return New(DsaKey(r), time.Unix(0,0), "Holder of "+issuer, denomination, "")
 }
 
 // Key id (fingerprint)