-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (76 loc) · 1.72 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"sort"
"strings"
"github.com/tonny-zhang/cotton"
)
type reqParam struct {
name string
val string
}
type reqParamList []reqParam
func (s reqParamList) Len() int { return len(s) }
func (s reqParamList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s reqParamList) Less(i, j int) bool {
result := strings.Compare(s[i].name, s[j].name)
if result == 0 {
result = strings.Compare(s[i].val, s[j].val)
}
return result < 0
}
func main() {
router := cotton.Default()
router.Use(func(ctx *cotton.Context) {
ctx.Response.Header().Add("Power by", "cotton")
})
{
g1 := router.Group("/sign")
g1.Use(func(ctx *cotton.Context) {
values := ctx.GetAllPostForm()
if nil != values {
list := make(reqParamList, 0)
var sign string
for k, v := range values {
if k == "sign" {
sign = v[0]
} else {
list = append(list, reqParam{k, v[0]})
}
}
if sign == "" {
ctx.JSON(http.StatusOK, cotton.M{
"code": 4000,
"errmsg": "not sign",
})
ctx.Abort()
return
}
sort.Sort(list)
strList := make([]string, 0)
for _, v := range list {
strList = append(strList, v.name+"="+v.val)
}
strSign := strings.Join(strList, "&")
h := md5.New()
h.Write([]byte(strSign))
signGet := hex.EncodeToString(h.Sum(nil))
fmt.Printf("[sign check] sign = %s, signGet = %s, str = %s\n", sign, signGet, strSign)
if sign != signGet {
ctx.JSON(http.StatusOK, cotton.M{
"code": 4001,
"errmsg": "sign error",
})
ctx.Abort()
}
}
})
g1.Post("/login", func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "login")
})
}
router.Run("")
}