这两天新调个接口的时候,一直提示签名错误,但所需的时间,工号,token都是对的,完全没有头绪,卡了两天后,终于找到原因了
原因是我的校验信息,是要将时间,工号,token封装在一起成字符串,然后用md5加密后传过去,之前封装时一直用的map,而map是无序的,转换成字符串时顺序不定,而后再用md5加密出来的字符串,跟所需字符串就不一样了
详看代码:
package main import ( "crypto/md5" "encoding/hex" "encoding/json" "fmt" "time" ) // 签名信息 type signa struct { ThirdTime int `json:"thirdTime"` ThirdCaller string `json:"thirdCaller"` ThirdToken string `json:"thirdToken"` } func main() { //校验信息 m := map[string]interface{}{ "thirdTime": int(time.Now().Unix() * 1000), "thirdCaller": "W9010315", "thirdToken": "55sfew4sfsf5rerx8sfwxs5", } signJson, _ := json.Marshal(m) fmt.Println(string(signJson)) // 计算MD5值 md55 := md5.Sum(signJson) thirdSign := hex.EncodeToString(md55[:]) fmt.Println(thirdSign) fmt.Println("########################") signs := signa{ ThirdTime: int(time.Now().Unix() * 1000), ThirdCaller: "W9010315", ThirdToken: "55sfew4sfsf5rerx8sfwxs5", } //用MD5加密签名信息 signJsons, _ := json.Marshal(signs) fmt.Println(string(signJsons)) // 计算MD5值 md555 := md5.Sum(signJsons) thirdSigns := hex.EncodeToString(md555[:]) fmt.Println(thirdSigns) }
输出结果如下,校验信息所需的字符串是0322那个,跟我用map加密出来的字符串完全不一样
{"thirdCaller":"W9010315","thirdTime":1701336910000,"thirdToken":"55sfew4sfsf5rerx8sfwxs5"} 92ef16ab18a81d0333dee9f01e456a05 ######################## {"thirdTime":1701336910000,"thirdCaller":"W9010315","thirdToken":"55sfew4sfsf5rerx8sfwxs5"} 032225f312a4b6200df96bd8d2b41ee4
标签:map,string,记录,fmt,json,Println,md5 From: https://www.cnblogs.com/Xinenhui/p/17868088.html