Skip to content

Commit

Permalink
feat: add Equal method to compare keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jun 28, 2019
1 parent c2dcc5c commit 80f7894
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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))
}
Expand Down
4 changes: 4 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 80f7894

Please sign in to comment.