gitee.com/lh-her-team/common@v1.5.1/helper/libp2pcrypto/openssl_common.go (about) 1 // +build openssl 2 3 package libp2pcrypto 4 5 import ( 6 "sync" 7 8 pb "github.com/libp2p/go-libp2p-core/crypto/pb" 9 10 openssl "github.com/libp2p/go-openssl" 11 ) 12 13 // define these as separate types so we can add more key types later and reuse 14 // code. 15 16 type opensslPublicKey struct { 17 key openssl.PublicKey 18 cacheLk sync.Mutex 19 cached []byte 20 } 21 22 type opensslPrivateKey struct { 23 key openssl.PrivateKey 24 } 25 26 func unmarshalOpensslPrivateKey(b []byte) (opensslPrivateKey, error) { 27 sk, err := openssl.LoadPrivateKeyFromDER(b) 28 if err != nil { 29 return opensslPrivateKey{}, err 30 } 31 return opensslPrivateKey{sk}, nil 32 } 33 34 func unmarshalOpensslPublicKey(b []byte) (opensslPublicKey, error) { 35 sk, err := openssl.LoadPublicKeyFromDER(b) 36 if err != nil { 37 return opensslPublicKey{}, err 38 } 39 return opensslPublicKey{key: sk, cached: b}, nil 40 } 41 42 // Verify compares a signature against input data 43 func (pk *opensslPublicKey) Verify(data, sig []byte) (bool, error) { 44 err := pk.key.VerifyPKCS1v15(openssl.SHA256_Method, data, sig) 45 return err == nil, err 46 } 47 48 func (pk *opensslPublicKey) Type() pb.KeyType { 49 switch pk.key.KeyType() { 50 case openssl.KeyTypeRSA: 51 return pb.KeyType_RSA 52 default: 53 return -1 54 } 55 } 56 57 // Bytes returns protobuf bytes of a public key 58 func (pk *opensslPublicKey) Bytes() ([]byte, error) { 59 pk.cacheLk.Lock() 60 var err error 61 if pk.cached == nil { 62 pk.cached, err = MarshalPublicKey(pk) 63 } 64 pk.cacheLk.Unlock() 65 return pk.cached, err 66 } 67 68 func (pk *opensslPublicKey) Raw() ([]byte, error) { 69 return pk.key.MarshalPKIXPublicKeyDER() 70 } 71 72 // Equals checks whether this key is equal to another 73 func (pk *opensslPublicKey) Equals(k Key) bool { 74 k0, ok := k.(*RsaPublicKey) 75 if !ok { 76 return basicEquals(pk, k) 77 } 78 return pk.key.Equal(k0.opensslPublicKey.key) 79 } 80 81 // Sign returns a signature of the input data 82 func (sk *opensslPrivateKey) Sign(message []byte) ([]byte, error) { 83 return sk.key.SignPKCS1v15(openssl.SHA256_Method, message) 84 } 85 86 // GetPublic returns a public key 87 func (sk *opensslPrivateKey) GetPublic() PubKey { 88 return &opensslPublicKey{key: sk.key} 89 } 90 91 func (sk *opensslPrivateKey) Type() pb.KeyType { 92 switch sk.key.KeyType() { 93 case openssl.KeyTypeRSA: 94 return pb.KeyType_RSA 95 default: 96 return -1 97 } 98 } 99 100 // Bytes returns protobuf bytes from a private key 101 func (sk *opensslPrivateKey) Bytes() ([]byte, error) { 102 return MarshalPrivateKey(sk) 103 } 104 105 func (sk *opensslPrivateKey) Raw() ([]byte, error) { 106 return sk.key.MarshalPKCS1PrivateKeyDER() 107 } 108 109 // Equals checks whether this key is equal to another 110 func (sk *opensslPrivateKey) Equals(k Key) bool { 111 k0, ok := k.(*RsaPrivateKey) 112 if !ok { 113 return basicEquals(sk, k) 114 } 115 return sk.key.Equal(k0.opensslPrivateKey.key) 116 }