-
Notifications
You must be signed in to change notification settings - Fork 3
/
sign.go
37 lines (34 loc) · 845 Bytes
/
sign.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
package nldyp
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/dtapps/go-library/utils/gorequest"
"github.com/dtapps/go-library/utils/gostring"
"github.com/dtapps/go-library/utils/gotime"
"sort"
"strings"
)
func (c *Client) Sign(p gorequest.Params) gorequest.Params {
p.Set("vendor", c.GetVendor())
p.Set("ts", gotime.Current().Timestamp())
// 排序所有的 key
var keys []string
for key := range p {
keys = append(keys, key)
}
sort.Strings(keys)
signStr := ""
for _, key := range keys {
signStr += key + gostring.GetString(p.Get(key))
}
p.Set("sign", createSign(fmt.Sprintf("%s%s%s", c.GetVendor(), p.Get("ts"), signStr)))
return p
}
// 签名
func createSign(signStr string) string {
h := md5.New()
h.Write([]byte(signStr))
cipherStr := h.Sum(nil)
return strings.ToUpper(hex.EncodeToString(cipherStr))
}