- 韵达签名
func YunDaSign(structData interface{}) ([]byte, string, string) {
jsonData, _ := json.Marshal(structData)
appKey := "你的key"
appSecret := "你的秘钥"
newStr := string(jsonData) + "_" + appSecret
md := md5.New()
md.Write([]byte(newStr))
md5Str := hex.EncodeToString(md.Sum(nil))
return jsonData, appKey, md5Str
}
- 京东签名
func JdSigin(body string, path string) (string, string, string, string, string) {
appKey := "你的key"
appSecret := "你的秘钥"
accessToken := "你的token"
timestamp := Util.GetDate()
algorithm := "md5-salt"
content := strings.Join([]string{
appSecret,
"access_token", accessToken,
"app_key", appKey,
"method", path,
"param_json", body,
"timestamp", timestamp,
"v", "2.0",
appSecret,
}, "")
sign, err := Sign(algorithm, []byte(content), appSecret)
if err != nil {
panic(err)
}
return sign, appKey, accessToken, timestamp, algorithm
}
func Sign(algorithm string, data []byte, secret string) (string, error) {
if algorithm == "md5-salt" {
h := md5.New()
if _, err := h.Write(data); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
} else if algorithm == "HMacMD5" {
h := hmac.New(md5.New, []byte(secret))
if _, err := h.Write(data); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
} else if algorithm == "HMacSHA1" {
h := hmac.New(sha1.New, []byte(secret))
if _, err := h.Write(data); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
} else if algorithm == "HMacSHA256" {
h := hmac.New(sha256.New, []byte(secret))
if _, err := h.Write(data); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
} else if algorithm == "HMacSHA512" {
h := hmac.New(sha512.New, []byte(secret))
if _, err := h.Write(data); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}
return "", fmt.Errorf("algorithm %s not supported yet", algorithm)
}
本文作者:
艾瑞可erik
本文链接: https://erik.xyz/2022/07/30/go-on-express/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!
本文链接: https://erik.xyz/2022/07/30/go-on-express/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!