From 80f7894f839319e84194db990c5a6f7781d27711 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 28 Jun 2019 13:37:09 +0200 Subject: [PATCH] feat: add Equal method to compare keys Closes #125 --- key.go | 7 +++++++ key_test.go | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/key.go b/key.go index 91ea98a7..3b36fa04 100644 --- a/key.go +++ b/key.go @@ -85,6 +85,9 @@ type PublicKey interface { // `KeyType() == KeyTypeRSA2` would both have `BaseType() == KeyTypeRSA`. BaseType() NID + // Equal compares the key with the passed in key. + Equal(key PublicKey) bool + evpPKey() *C.EVP_PKEY } @@ -109,6 +112,10 @@ type pKey struct { func (key *pKey) evpPKey() *C.EVP_PKEY { return key.key } +func (key *pKey) Equal(other PublicKey) bool { + return C.EVP_PKEY_cmp(key.key, other.evpPKey()) == 1 +} + func (key *pKey) KeyType() NID { return NID(C.EVP_PKEY_id(key.key)) } diff --git a/key_test.go b/key_test.go index 753e3784..56541981 100644 --- a/key_test.go +++ b/key_test.go @@ -36,6 +36,10 @@ func TestMarshal(t *testing.T) { t.Fatal(err) } + if !key.Equal(key) { + t.Fatal("key not equal to itself") + } + privateBlock, _ := pem_pkg.Decode(keyBytes) key, err = LoadPrivateKeyFromDER(privateBlock.Bytes) if err != nil {