Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from dignifiedquire/feat/key-equals
Browse files Browse the repository at this point in the history
feat: add Equal method to compare keys
  • Loading branch information
Stebalien authored Jun 29, 2019
2 parents 01a7c5e + 80f7894 commit 0298788
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 0298788

Please sign in to comment.