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
}
