智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
589 B

  1. package utils
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "sort"
  6. "strings"
  7. )
  8. // CheckSignature 微信公众号签名检查
  9. func CheckSignature(signature, timestamp, nonce, token string) bool {
  10. arr := []string{timestamp, nonce, token}
  11. // 字典序排序
  12. sort.Strings(arr)
  13. n := len(timestamp) + len(nonce) + len(token)
  14. var b strings.Builder
  15. b.Grow(n)
  16. for i := 0; i < len(arr); i++ {
  17. b.WriteString(arr[i])
  18. }
  19. return Sha1(b.String()) == signature
  20. }
  21. // 进行Sha1编码
  22. func Sha1(str string) string {
  23. h := sha1.New()
  24. h.Write([]byte(str))
  25. return hex.EncodeToString(h.Sum(nil))
  26. }