|
- package utils
-
- import (
- "crypto/sha1"
- "encoding/hex"
- "sort"
- "strings"
- )
-
- // CheckSignature 微信公众号签名检查
- func CheckSignature(signature, timestamp, nonce, token string) bool {
- arr := []string{timestamp, nonce, token}
- // 字典序排序
- sort.Strings(arr)
-
- n := len(timestamp) + len(nonce) + len(token)
- var b strings.Builder
- b.Grow(n)
- for i := 0; i < len(arr); i++ {
- b.WriteString(arr[i])
- }
-
- return Sha1(b.String()) == signature
- }
-
- // 进行Sha1编码
- func Sha1(str string) string {
- h := sha1.New()
- h.Write([]byte(str))
- return hex.EncodeToString(h.Sum(nil))
- }
|