-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
58 lines (48 loc) · 1.03 KB
/
util.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
package main
import (
"compress/zlib"
"crypto/md5"
"fmt"
"io"
"sort"
"strings"
"bytes"
"io/ioutil"
)
func formatParams(params map[string]string) string {
argsNum := len(params)
keys := make([]string, argsNum)
args := make([]string, argsNum)
index := 0
for k := range params {
keys[index] = k
index++
}
sort.Strings(keys)
for i, k := range keys {
args[i] = fmt.Sprintf("%s=%s", k, params[k])
}
return strings.Join(args, "^_^")
}
func hashSign(origin string) string {
md5Hash := md5.New()
io.WriteString(md5Hash, origin)
return fmt.Sprintf("%x", md5Hash.Sum(nil))
}
func compress(data []byte) ([]byte, error) {
reader := bytes.NewReader(data)
z, err := zlib.NewReader(reader)
if err != nil {
return nil, err
}
defer z.Close()
p, err := ioutil.ReadAll(z)
if err != nil {
return nil, err
}
return p, nil
}
func generatorSign(params map[string]string, data []byte, secret string) string {
var source []string = []string{formatParams(params), string(data), secret}
return hashSign(strings.Join(source, "^_^"))
}