微信公众号验证服务器地址的有效性的golang版本代码实现演示
import (
"crypto/sha1"
"xxx/config"
"xxx/modules/log"
"xxx/modules/server"
"xxx/modules/utils"
"encoding/hex"
"io"
"sort"
"strings"
)
func Check(c *server.Context) {
log.InfoLogger.H("Check", utils.H{
"query": c.Request.URL.Query(),
})
echostr := c.Request.URL.Query().Get("echostr")
nonce := c.Request.URL.Query().Get("nonce")
signature := c.Request.URL.Query().Get("signature")
timestamp := c.Request.URL.Query().Get("timestamp")
token := config.Token
tmpSlice := []string{
token,
timestamp,
nonce,
}
sort.Strings(tmpSlice)
tmpStr := strings.Join(tmpSlice, "")
h := sha1.New()
_, err := io.WriteString(h, tmpStr)
if err != nil {
return
}
sign := hex.EncodeToString(h.Sum(nil))
if sign == signature {
log.InfoLogger.H("Check", utils.H{
"status": "ok",
})
c.Data(200, "text/html; charset=utf-8", []byte(echostr))
return
}
c.Data(200, "text/html; charset=utf-8", []byte("fail"))
return
}
版权声明
由 durban创作并维护的 小绒毛的足迹博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于
博客(
https://www.xiaorongmao.com ),版权所有,侵权必究。