passforios-gopenpgp/key/fingerprint.go

34 lines
795 B
Go
Raw Normal View History

package key
2018-06-04 16:05:14 -07:00
import (
"bytes"
"encoding/hex"
"errors"
2018-11-01 17:03:43 +01:00
"github.com/ProtonMail/go-pm-crypto/armor"
2018-06-04 16:05:14 -07:00
"golang.org/x/crypto/openpgp"
)
// GetFingerprint get a armored public key fingerprint
func GetFingerprint(publicKey string) (string, error) {
2018-09-19 11:52:14 +02:00
rawPubKey, err := armor.Unarmor(publicKey)
2018-06-04 16:05:14 -07:00
if err != nil {
return "", err
}
return GetFingerprintBinKey(rawPubKey)
}
// GetFingerprintBinKey get a unarmored public key fingerprint
func GetFingerprintBinKey(publicKey []byte) (string, error) {
pubKeyReader := bytes.NewReader(publicKey)
pubKeyEntries, err := openpgp.ReadKeyRing(pubKeyReader)
if err != nil {
return "", err
}
for _, e := range pubKeyEntries {
fp := e.PrimaryKey.Fingerprint
return hex.EncodeToString(fp[:]), nil
}
return "", errors.New("Can't find public key")
}