mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
crypto: abstracted Key and added Equals.
This commit is contained in:
parent
c7bd8b78dd
commit
484d6004f7
@ -23,7 +23,17 @@ const (
|
||||
RSA = iota
|
||||
)
|
||||
|
||||
type Key interface {
|
||||
// Bytes returns a serialized, storeable representation of this key
|
||||
Bytes() ([]byte, error)
|
||||
|
||||
// Equals checks whether two PubKeys are the same
|
||||
Equals(Key) bool
|
||||
}
|
||||
|
||||
type PrivKey interface {
|
||||
Key
|
||||
|
||||
// Cryptographically sign the given bytes
|
||||
Sign([]byte) ([]byte, error)
|
||||
|
||||
@ -32,17 +42,13 @@ type PrivKey interface {
|
||||
|
||||
// Generate a secret string of bytes
|
||||
GenSecret() []byte
|
||||
|
||||
// Bytes returns a serialized, storeable representation of this key
|
||||
Bytes() ([]byte, error)
|
||||
}
|
||||
|
||||
type PubKey interface {
|
||||
Key
|
||||
|
||||
// Verify that 'sig' is the signed hash of 'data'
|
||||
Verify(data []byte, sig []byte) (bool, error)
|
||||
|
||||
// Bytes returns a serialized, storeable representation of this key
|
||||
Bytes() ([]byte, error)
|
||||
}
|
||||
|
||||
// Given a public key, generates the shared key.
|
||||
@ -229,3 +235,14 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) {
|
||||
return nil, ErrBadKeyType
|
||||
}
|
||||
}
|
||||
|
||||
// KeyEqual checks whether two
|
||||
func KeyEqual(k1, k2 Key) bool {
|
||||
if k1 == k2 {
|
||||
return true
|
||||
}
|
||||
|
||||
b1, err1 := k1.Bytes()
|
||||
b2, err2 := k2.Bytes()
|
||||
return bytes.Equal(b1, b2) && err1 == err2
|
||||
}
|
||||
|
||||
@ -3,12 +3,14 @@ package crypto
|
||||
import "testing"
|
||||
|
||||
func TestRsaKeys(t *testing.T) {
|
||||
sk, _, err := GenerateKeyPair(RSA, 512)
|
||||
sk, pk, err := GenerateKeyPair(RSA, 512)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testKeySignature(t, sk)
|
||||
testKeyEncoding(t, sk)
|
||||
testKeyEquals(t, sk)
|
||||
testKeyEquals(t, pk)
|
||||
}
|
||||
|
||||
func testKeySignature(t *testing.T, sk PrivKey) {
|
||||
@ -52,3 +54,41 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testKeyEquals(t *testing.T, k Key) {
|
||||
kb, err := k.Bytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !KeyEqual(k, k) {
|
||||
t.Fatal("Key not equal to itself.")
|
||||
}
|
||||
|
||||
if !KeyEqual(k, testkey(kb)) {
|
||||
t.Fatal("Key not equal to key with same bytes.")
|
||||
}
|
||||
|
||||
sk, pk, err := GenerateKeyPair(RSA, 512)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if KeyEqual(k, sk) {
|
||||
t.Fatal("Keys should not equal.")
|
||||
}
|
||||
|
||||
if KeyEqual(k, pk) {
|
||||
t.Fatal("Keys should not equal.")
|
||||
}
|
||||
}
|
||||
|
||||
type testkey []byte
|
||||
|
||||
func (pk testkey) Bytes() ([]byte, error) {
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
func (pk testkey) Equals(k Key) bool {
|
||||
return KeyEqual(pk, k)
|
||||
}
|
||||
|
||||
@ -41,6 +41,11 @@ func (pk *RsaPublicKey) Bytes() ([]byte, error) {
|
||||
return proto.Marshal(pbmes)
|
||||
}
|
||||
|
||||
// Equals checks whether this key is equal to another
|
||||
func (pk *RsaPublicKey) Equals(k Key) bool {
|
||||
return KeyEqual(pk, k)
|
||||
}
|
||||
|
||||
func (sk *RsaPrivateKey) GenSecret() []byte {
|
||||
buf := make([]byte, 16)
|
||||
rand.Read(buf)
|
||||
@ -65,6 +70,11 @@ func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
|
||||
return proto.Marshal(pbmes)
|
||||
}
|
||||
|
||||
// Equals checks whether this key is equal to another
|
||||
func (sk *RsaPrivateKey) Equals(k Key) bool {
|
||||
return KeyEqual(sk, k)
|
||||
}
|
||||
|
||||
func UnmarshalRsaPrivateKey(b []byte) (*RsaPrivateKey, error) {
|
||||
sk, err := x509.ParsePKCS1PrivateKey(b)
|
||||
if err != nil {
|
||||
|
||||
@ -379,17 +379,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error)
|
||||
// did have pubkey, let's verify it's really the same.
|
||||
// this shouldn't ever happen, given we hashed, etc, but it could mean
|
||||
// expected code (or protocol) invariants violated.
|
||||
|
||||
lb, err1 := npeer.PubKey.Bytes()
|
||||
if err1 != nil {
|
||||
return nil, err1
|
||||
}
|
||||
rb, err2 := rpk.Bytes()
|
||||
if err2 != nil {
|
||||
return nil, err2
|
||||
}
|
||||
|
||||
if !bytes.Equal(lb, rb) {
|
||||
if !npeer.PubKey.Equals(rpk) {
|
||||
return nil, fmt.Errorf("WARNING: PubKey mismatch: %v", npeer.ID.Pretty())
|
||||
}
|
||||
return npeer, nil
|
||||
|
||||
Loading…
Reference in New Issue
Block a user