package controller import ( "crypto/sha1" "encoding/hex" "fmt" "github.com/gin-gonic/gin" "sort" ) const ( TOKEN = "QoI6tG856JFGLZaWy2ljffJlkBOYeU" ENCODINGAESKEY = "QoI6tG856JFGLZaWy2ljffJlkBOYeU6GSfGuzpt18Pu" ) //func checkout(response http.ResponseWriter, request *http.Request) { func WxMsg(c *gin.Context) { // 获取参数 signature := c.PostForm("signature") timestamp := c.PostForm("timestamp") nonce := c.PostForm("nonce") echostr := c.PostForm("echostr") //timestamp := request.FormValue("echostr") //nonce := request.FormValue("nonce") //echostr := request.FormValue("echostr") //将token、timestamp、nonce三个参数进行字典序排序 var tempArray = []string{TOKEN, timestamp, nonce} sort.Strings(tempArray) //将三个参数字符串拼接成一个字符串进行sha1加密 var sha1String string = "" for _, v := range tempArray { sha1String += v } h := sha1.New() h.Write([]byte(sha1String)) sha1String = hex.EncodeToString(h.Sum([]byte(""))) //获得加密后的字符串可与signature对比 if sha1String == signature { _, err := c.Writer.Write([]byte(echostr)) if err != nil { fmt.Println("响应失败。。。") } } else { fmt.Println("验证失败") } }