-
Notifications
You must be signed in to change notification settings - Fork 7
/
sign.go
92 lines (79 loc) · 1.51 KB
/
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
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
87
88
89
90
91
92
package unionpay
import (
"crypto/md5"
"crypto/sha1"
"errors"
"fmt"
"io"
"sort"
"strings"
)
type KVpair struct {
K, V string
}
type KVpairs []KVpair
func (t KVpairs) Less(i, j int) bool {
return t[i].K < t[j].K
}
func (t KVpairs) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (t KVpairs) Len() int {
return len(t)
}
func (t KVpairs) Sort() KVpairs {
sort.Sort(t)
return t
}
func (t KVpairs) RemoveEmpty() KVpairs {
for i := 0; i < len(t); i++ {
if t[i].V == "" {
t = append(t[:i], t[i+1:]...)
i--
}
}
return t
}
func (t KVpairs) Join(sep string) string {
var strs []string
for _, kv := range t {
strs = append(strs, kv.K+"="+kv.V)
}
return strings.Join(strs, sep)
}
func MD5(strs ...string) string {
h := md5.New()
for _, str := range strs {
io.WriteString(h, str)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func SHA1(b []byte) []byte {
h := sha1.New()
h.Write(b)
return h.Sum(nil)
}
func GenKVpairs(paramsKeyMap map[string]bool, initParams map[string]string, skipKey ...string) (kvs KVpairs, err error) {
kvs = make(KVpairs, 0)
for key, isMust := range paramsKeyMap {
val, ok := initParams[key]
if ok && val != "" {
kvs = append(kvs, KVpair{K: key, V: val})
} else {
// sign 参数需要签名后才会生成这里跳过
if isMust && !Contains(skipKey, key) {
err = errors.New("must param is empty:" + key)
return
}
}
}
return
}
func Contains(strs []string, key string) bool {
for _, v := range strs {
if v == key {
return true
}
}
return false
}