-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
48 lines (36 loc) · 787 Bytes
/
example.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
40
41
42
43
44
45
46
47
48
package main
import (
"crypto/sha1"
"github.com/gin-gonic/gin"
"net/http"
"sort"
"strings"
)
const (
token = "weixin"
)
func checkSignature(c *gin.Context) {
signature := c.Param("signature")
timestamp := c.Param("timestamp")
nonce := c.Param("nonce")
echostr := c.Param("echostr")
if check(signature, timestamp, nonce) {
c.String(200, echostr)
}
c.String(500, "Bad Signature")
}
func check(signature, timestamp, nonce string) bool {
arr := []string{token, timestamp, nonce}
sort.Strings(arr)
data := sha1.Sum([]byte(strings.Join(arr, "")))
return string(data[:]) == signature
}
func handleMsg(c *gin.Context) {
}
func main() {
g := gin.Default()
g.GET("/", func(context *gin.Context) {
checkSignature(context)
})
http.ListenAndServe(":8080", nil)
}