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

feat: add Equal method to compare keys #1

Merged
merged 1 commit into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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