Commit 5245b27e authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

openpgp: add PublicKey KeyId string accessors

R=agl, agl1
CC=golang-dev
https://golang.org/cl/4297041
parent a5697251
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/sha1" "crypto/sha1"
"encoding/binary" "encoding/binary"
"fmt"
"hash" "hash"
"io" "io"
"os" "os"
...@@ -239,6 +240,18 @@ func (pk *PublicKey) VerifyUserIdSignature(id string, sig *Signature) (err os.Er ...@@ -239,6 +240,18 @@ func (pk *PublicKey) VerifyUserIdSignature(id string, sig *Signature) (err os.Er
return pk.VerifySignature(h, sig) 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 // 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 // bit length that was specified in the original input. This allows the MPI to
// be reserialized exactly. // be reserialized exactly.
......
...@@ -16,9 +16,11 @@ var pubKeyTests = []struct { ...@@ -16,9 +16,11 @@ var pubKeyTests = []struct {
creationTime uint32 creationTime uint32
pubKeyAlgo PublicKeyAlgorithm pubKeyAlgo PublicKeyAlgorithm
keyId uint64 keyId uint64
keyIdString string
keyIdShort string
}{ }{
{rsaPkDataHex, rsaFingerprintHex, 0x4d3c5c10, PubKeyAlgoRSA, 0xa34d7e18c20c31bb}, {rsaPkDataHex, rsaFingerprintHex, 0x4d3c5c10, PubKeyAlgoRSA, 0xa34d7e18c20c31bb, "A34D7E18C20C31BB", "C20C31BB"},
{dsaPkDataHex, dsaFingerprintHex, 0x4d432f89, PubKeyAlgoDSA, 0x8e8fbe54062f19ed}, {dsaPkDataHex, dsaFingerprintHex, 0x4d432f89, PubKeyAlgoDSA, 0x8e8fbe54062f19ed, "8E8FBE54062F19ED", "062F19ED"},
} }
func TestPublicKeyRead(t *testing.T) { func TestPublicKeyRead(t *testing.T) {
...@@ -46,6 +48,12 @@ func TestPublicKeyRead(t *testing.T) { ...@@ -46,6 +48,12 @@ func TestPublicKeyRead(t *testing.T) {
if pk.KeyId != test.keyId { if pk.KeyId != test.keyId {
t.Errorf("#%d: bad keyid got:%x want:%x", i, 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)
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment