go语言实现招商银行支付签名验证功能流程步骤
招商银行的签名验证主要是使用国密sm2作为签名验证,同时对生成的的签名数据 r,s做了16进制转换的特殊处理,所以默认的sm2生成的验证数据会有问题。
这里使用go-cryptobin加密解密库,go-cryptobin是go的常用加密解密库:
go-cryptobin 包括常用的对称加密和非对称加密及签名验证
对称加密解密(Aes/Des/TriDes/SM4/Tea/Twofish/Xts)
对称加密解密模式(ECB/CBC/CFB/OFB/CTR/GCM)
对称加密解密补码(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO7816_4Padding/TBCPadding/PKCS1Padding)
非对称加密解密(RSA/SM2)
非对称签名验证(RSA/PSS/DSA/Ecdsa/EdDSA/SM2)
默认 Aes, ECB, NoPadding
以下是go版本的招商银行签名验证实现步骤
1.安装go-cryptobin包
go get -u github.com/deatil/go-cryptobin
2.签名验证使用
package main
import (
"fmt"
"encoding/base64"
cryptobin_sm2 "github.com/deatil/go-cryptobin/cryptobin/sm2"
)
func main() {
//sm2签名【招商银行】
sm2key := "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E="
sm2keyBytes, _ := base64.StdEncoding.DecodeString(sm2key)
sm2data := `{"request":{"body":{"TEST":"中文","TEST2":"!@#$%^&*()","TEST3":12345,"TEST4":[{"arrItem1":"qaz","arrItem2":123,"arrItem3":true,"arrItem4":"中文"}],"buscod":"N02030"},"head":{"funcode":"DCLISMOD","userid":"N003261207"}},"signature":{"sigdat":"__signature_sigdat__"}}`
sm2userid := "N0033511370000000000000000"
sm2userid = sm2userid[0:16]
sm2Sign := cryptobin_sm2.NewSM2().
FromPrivateKeyBytes(sm2keyBytes).
FromString(sm2data).
SignHex([]byte(sm2userid)).
ToBase64String()
//sm2验证【招商银行】
sm2signdata := "CDAYcxm3jM+65XKtFNii0tKrTmEbfNdR/Q/BtuQFzm5+luEf2nAhkjYTS2ygPjodpuAkarsNqjIhCZ6+xD4WKA=="
sm2Very := cryptobin_sm2.NewSM2().
FromBase64String(sm2signdata).
FromPrivateKeyBytes(sm2keyBytes).
MakePublicKey().
VerifyHex([]byte(sm2data), []byte(sm2userid)).
ToVeryed()
fmt.Println("签名结果:", sm2Sign)
fmt.Println("验证结果:", sm2Very)
}
go-cryptobin包github:
https://github.com/deatil/go-cryptobin
go-cryptobin包github所有文档:
https://pkg.go.dev/github.com/deatil/go-cryptobin
go-cryptobin包github常用文档:
https://github.com/deatil/go-cryptobin/tree/main/docs
相关文章