github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/gmsm/signature.go (about) 1 // Package gmsm 2 // 3 // @author: xwc1125 4 package gmsm 5 6 import ( 7 "crypto/ecdsa" 8 "crypto/rand" 9 10 "github.com/tjfoc/gmsm/sm2" 11 ) 12 13 // Sign 使用sm2进行签名 14 func Sign(prv *ecdsa.PrivateKey, msg []byte) (sig []byte, err error) { 15 privateKey := NewPrivateKey(prv.D) 16 r, b, err := sm2.Sm2Sign(privateKey, msg, nil, rand.Reader) 17 if err != nil { 18 return nil, err 19 } 20 21 return sm2.SignDigitToSignData(r, b) 22 } 23 24 // Verify 使用sm2 公钥验证签名 25 func Verify(pub *ecdsa.PublicKey, msg []byte, signature []byte) bool { 26 r, s, err := sm2.SignDataToSignDigit(signature) 27 if err != nil { 28 return false 29 } 30 return sm2.Sm2Verify(FromECDSAPubKey(pub), msg, nil, r, s) 31 }