Add ToPublic method to get a public key object from a private key (#65)

This commit is contained in:
wussler 2020-07-22 17:13:23 +02:00 committed by GitHub
parent 48f05401ce
commit 979fdb3f4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View file

@ -359,6 +359,21 @@ func (key *Key) GetEntity() *openpgp.Entity {
return key.entity
}
// ToPublic returns the corresponding public key of the given private key.
func (key *Key) ToPublic() (publicKey *Key, err error) {
if !key.IsPrivate() {
return nil, errors.New("gopenpgp: key is already public")
}
publicKey, err = key.Copy()
if err != nil {
return nil, err
}
publicKey.ClearPrivateParams()
return
}
// --- Internal methods
// getSHA256FingerprintBytes computes the SHA256 fingerprint of a public key