github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/prime256v1/crypto.go (about)

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