// 签名函数,仅作为示例
func sign(params map[string]interface{}, privateKey string) string {
// 检查并转换私钥格式
if !strings.HasPrefix(privateKey, "-----") {
privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + privateKey + "\n-----END RSA PRIVATE KEY-----"
}
// 过滤掉值为空的参数
keys := make([]string, 0, len(params))
for k, v := range params {
if v == nil {
continue
}
if str, ok := v.(string); ok && str == "" {
continue
}
keys = append(keys, k)
}
// 对参数进行排序并连接成一个字符串
sort.Strings(keys)
paramList := make([]string, 0, len(keys))
for _, key := range keys {
paramList = append(paramList, fmt.Sprintf("%s=%v", key, params[key]))
}
content := strings.Join(paramList, "&")
// 获取私钥对象
block, _ := pem.Decode([]byte(privateKey))
priKey, _ := x509.ParsePKCS8PrivateKey(block.Bytes)
// 对连接的字符串进行签名
h := crypto.Hash.New(crypto.SHA1)
h.Write([]byte(content))
hashed := h.Sum(nil)
rb, _ := rsa.SignPKCS1v15(rand.Reader, priKey.(*rsa.PrivateKey), crypto.SHA1, hashed)
return base64.StdEncoding.EncodeToString(rb)
}
func buildDemoMessage() map[string]interface{} {
params := map[string]interface{}{
"attach": "",
"body": "yujing_test",
"clientIp": "2a01:e0a:3af:a010:1086:b026:67dd:bb1d",
"currency": "CNY",
"detail": "",
"extra": "{\"appId\":\"\",\"openId\":\"oNzHb6upfqZxpUoaeIgmmop4nScI\"}",
"isNeedShare": "false",
"mchId": "93751497",
"mchMemberInfo": "",
"outTradeNo": "p0bihp26hidb4ua5x1qm",
"subMchId": "",
"timeExpire": "20210715100526",
"totalFee": "3",
"tradeType": "wx_jsapi",
"notifyUrl": "https://onebuytest.shengpay.com/main/notify",
"nonceStr": "XCHNIWGFKGWR",
"signType": "RSA",
}
sign := sign(params, "your private key")
params["sign"] = sign
return params
}