forked from omigo/weixin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
39 lines (30 loc) · 961 Bytes
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package weixin
import (
"crypto/sha1"
"fmt"
"sort"
"strings"
"github.com/arstd/log"
)
// ValidateURL 验证 URL 以判断来源是否合法
func ValidateURL(token, timestamp, nonce, signature string) bool {
tmpArr := []string{token, timestamp, nonce}
sort.Strings(tmpArr)
tmpStr := strings.Join(tmpArr, "")
actual := fmt.Sprintf("%x", sha1.Sum([]byte(tmpStr)))
log.Tracef("%s %s", tmpArr, actual)
return actual == signature
}
// Signature 对加密的报文计算签名
func Signature(token, timestamp, nonce, encrypt string) string {
tmpArr := []string{token, timestamp, nonce, encrypt}
sort.Strings(tmpArr)
tmpStr := strings.Join(tmpArr, "")
actual := fmt.Sprintf("%x", sha1.Sum([]byte(tmpStr)))
log.Tracef("%s %s", tmpArr, actual)
return actual
}
// CheckSignature 验证加密的报文的签名
func CheckSignature(token, timestamp, nonce, encrypt, sign string) bool {
return Signature(token, timestamp, nonce, encrypt) == sign
}