]> Cypherpunks repositories - gostls13.git/commitdiff
openpgp: add PublicKey KeyId string accessors
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 15 Mar 2011 17:06:17 +0000 (10:06 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 15 Mar 2011 17:06:17 +0000 (10:06 -0700)
R=agl, agl1
CC=golang-dev
https://golang.org/cl/4297041

src/pkg/crypto/openpgp/packet/public_key.go
src/pkg/crypto/openpgp/packet/public_key_test.go

index daf5a1e6643bf28a897d2ddd559765904cba1e8f..9b6dc4aa12e17683aa6c25c389a1c1ed4f2c6d7a 100644 (file)
@@ -11,6 +11,7 @@ import (
        "crypto/rsa"
        "crypto/sha1"
        "encoding/binary"
+       "fmt"
        "hash"
        "io"
        "os"
@@ -239,6 +240,18 @@ func (pk *PublicKey) VerifyUserIdSignature(id string, sig *Signature) (err os.Er
        return pk.VerifySignature(h, sig)
 }
 
+// KeyIdString returns the public key's fingerprint in capital hex
+// (e.g. "6C7EE1B8621CC013").
+func (pk *PublicKey) KeyIdString() string {
+       return fmt.Sprintf("%X", pk.Fingerprint[12:20])
+}
+
+// KeyIdShortString returns the short form of public key's fingerprint
+// in capital hex, as shown by gpg --list-keys (e.g. "621CC013").
+func (pk *PublicKey) KeyIdShortString() string {
+       return fmt.Sprintf("%X", pk.Fingerprint[16:20])
+}
+
 // A parsedMPI is used to store the contents of a big integer, along with the
 // bit length that was specified in the original input. This allows the MPI to
 // be reserialized exactly.
index c015f64aec965005861863fbb45945d48a99224d..069388c14dccc1fc705e2b476faa9d29c3b2939b 100644 (file)
@@ -16,9 +16,11 @@ var pubKeyTests = []struct {
        creationTime   uint32
        pubKeyAlgo     PublicKeyAlgorithm
        keyId          uint64
+       keyIdString    string
+       keyIdShort     string
 }{
-       {rsaPkDataHex, rsaFingerprintHex, 0x4d3c5c10, PubKeyAlgoRSA, 0xa34d7e18c20c31bb},
-       {dsaPkDataHex, dsaFingerprintHex, 0x4d432f89, PubKeyAlgoDSA, 0x8e8fbe54062f19ed},
+       {rsaPkDataHex, rsaFingerprintHex, 0x4d3c5c10, PubKeyAlgoRSA, 0xa34d7e18c20c31bb, "A34D7E18C20C31BB", "C20C31BB"},
+       {dsaPkDataHex, dsaFingerprintHex, 0x4d432f89, PubKeyAlgoDSA, 0x8e8fbe54062f19ed, "8E8FBE54062F19ED", "062F19ED"},
 }
 
 func TestPublicKeyRead(t *testing.T) {
@@ -46,6 +48,12 @@ func TestPublicKeyRead(t *testing.T) {
                if pk.KeyId != test.keyId {
                        t.Errorf("#%d: bad keyid got:%x want:%x", i, pk.KeyId, test.keyId)
                }
+               if g, e := pk.KeyIdString(), test.keyIdString; g != e {
+                       t.Errorf("#%d: bad KeyIdString got:%q want:%q", i, g, e)
+               }
+               if g, e := pk.KeyIdShortString(), test.keyIdShort; g != e {
+                       t.Errorf("#%d: bad KeyIdShortString got:%q want:%q", i, g, e)
+               }
        }
 }