voucher/internal/pkg/cmb/encrypt/sm3.go

37 lines
955 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package encrypt
import (
"crypto/rand"
em2 "github.com/emmansun/gmsm/sm2"
)
// SM3WithSM2Sign SM3WithSM2签名 Hex ToUpper
func SM3WithSM2Sign(key string, data string) ([]byte, error) {
//掌上生活文档是错误的实际只需要对data进行本身进行签名而不需要对data的sm3摘要进行签名
//hashed := sm3.Sm3Sum([]byte(data))
// 获取私钥
//hashed := em3.Sum([]byte(data))
// 获取私钥
privateKey, err := ParsePrivateKey(key, Hex)
if err != nil {
return nil, err
}
signed, err := privateKey.Sign(rand.Reader, []byte(data), em2.DefaultSM2SignerOpts)
return signed, nil
}
func Sm2Sm3VerySign(data, sign string, pubKey string) (bool, error) {
// 计算hash值
//hashed := em3.Sum([]byte(data))
// 获取公钥
publicKey, err := ParsePublicKey(pubKey, Hex)
if err != nil {
return false, err
}
// 验证签名
ok := em2.VerifyASN1WithSM2(publicKey, nil, []byte(data), []byte(sign))
return ok, nil
}