github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/secp256k1/crypto.go (about) 1 // Package secp256k1 2 // 3 // @author: xwc1125 4 package secp256k1 5 6 import ( 7 "crypto" 8 "crypto/ecdsa" 9 "crypto/elliptic" 10 "crypto/rand" 11 "hash" 12 13 crypto2 "github.com/chain5j/chain5j-pkg/crypto" 14 "github.com/chain5j/chain5j-pkg/crypto/hashalg/sha3" 15 "github.com/chain5j/chain5j-pkg/crypto/signature/secp256k1/btcecv1" 16 ) 17 18 var ( 19 _ crypto2.ECDSA = new(Secp251k1) 20 ) 21 22 type Secp251k1 struct { 23 } 24 25 func (s Secp251k1) GenerateKey(curve elliptic.Curve) (*ecdsa.PrivateKey, error) { 26 return GenerateKey() 27 } 28 29 func (s Secp251k1) HashType(curveName string) crypto.Hash { 30 return crypto.SHA3_256 31 } 32 33 func (s Secp251k1) HashFunc(cryptoName string) func() hash.Hash { 34 return sha3.NewKeccak256 35 } 36 37 func (s Secp251k1) HashMsg(cryptoName string, data []byte) ([]byte, error) { 38 return sha3.Keccak256(data), nil 39 } 40 41 func (s Secp251k1) ToECDSA(prv crypto.PrivateKey) *ecdsa.PrivateKey { 42 return prv.(*btcecv1.PrivateKey).ToECDSA() 43 } 44 45 func (s Secp251k1) FromECDSA(prv *ecdsa.PrivateKey) crypto.PrivateKey { 46 return (*btcecv1.PrivateKey)(prv) 47 } 48 49 func (s Secp251k1) ToECDSAPubKey(pub crypto.PublicKey) *ecdsa.PublicKey { 50 publicKey := pub.(*btcecv1.PublicKey) 51 return publicKey.ToECDSA() 52 } 53 54 func (s Secp251k1) FromECDSAPubKey(pub *ecdsa.PublicKey) crypto.PublicKey { 55 return (*btcecv1.PublicKey)(pub) 56 } 57 58 func (s Secp251k1) MarshalPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) { 59 return MarshalPrivateKey(key) 60 } 61 62 func (s Secp251k1) UnmarshalPrivateKey(curve elliptic.Curve, keyBytes []byte) (*ecdsa.PrivateKey, error) { 63 return UnmarshalPrivateKey(curve, keyBytes) 64 } 65 66 func (s Secp251k1) MarshalPublicKey(pub *ecdsa.PublicKey) ([]byte, error) { 67 return MarshalPublicKey(pub) 68 } 69 70 func (s Secp251k1) UnmarshalPublicKey(curve elliptic.Curve, data []byte) (*ecdsa.PublicKey, error) { 71 return UnmarshalPublicKey(curve, data) 72 } 73 74 func (s Secp251k1) MarshalPrivateKeyX509(key *ecdsa.PrivateKey) ([]byte, error) { 75 return MarshalPrivateKeyX509(key) 76 } 77 78 func (s Secp251k1) UnmarshalPrivateKeyX509(curve elliptic.Curve, keyBytes []byte) (*ecdsa.PrivateKey, error) { 79 return UnmarshalPrivateKeyX509(curve, keyBytes) 80 } 81 82 func (s Secp251k1) MarshalPublicKeyX509(pub *ecdsa.PublicKey) ([]byte, error) { 83 return MarshalPublicKeyX509(pub) 84 } 85 86 func (s Secp251k1) UnmarshalPublicKeyX509(curve elliptic.Curve, data []byte) (*ecdsa.PublicKey, error) { 87 return UnmarshalPublicKeyX509(curve, data) 88 } 89 90 func (s Secp251k1) Sign(prv *ecdsa.PrivateKey, hash []byte) (sig []byte, err error) { 91 return Sign(prv, hash) 92 } 93 94 func (s Secp251k1) Verify(pub *ecdsa.PublicKey, hash []byte, signature []byte) bool { 95 return Verify(pub, hash, signature) 96 } 97 98 // GenerateKey 生成s256的私钥 99 func GenerateKey() (*ecdsa.PrivateKey, error) { 100 return ecdsa.GenerateKey(S256(), rand.Reader) 101 }