标签:ack package main time 拼接 range 图片 handle 9.png
参数 | 描述 |
---|---|
signature | 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 |
timestamp | 时间戳 |
nonce | 随机数 |
echostr | 随机字符串 |
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"net/http"
"sort"
)
func main() {
// 绑定路由
http.HandleFunc("/", checkout)
// 启动监听=j
err := http.ListenAndServe(":3456", nil)
if err != nil {
fmt.Println("服务器启动失败!")
}
}
func checkout(response http.ResponseWriter, request *http.Request) {
//解析URL参数
err := request.ParseForm()
if err != nil {
fmt.Println("URL解析失败!")
return
}
// token
var token string = "iwuqing"
// 获取参数
signature := request.FormValue("signature")
timestamp := request.FormValue("timestamp")
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 := response.Write([]byte(echostr))
if err != nil {
fmt.Println("响应失败。。。")
}
} else {
fmt.Println("验证失败")
}
}
https://github.com/iwuqing/WeChat-with-Go
标签:ack package main time 拼接 range 图片 handle 9.png
原文地址:https://www.cnblogs.com/iwuqing/p/10296391.html