-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.go
77 lines (63 loc) · 1.63 KB
/
signature.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
package csb
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"hash"
"io"
"sort"
)
// paramSorter 排序容器
type paramSorter struct {
Keys []string
Values []string
}
// params map 转换为paramsSorter格式
func newParamsSorter(m map[string]string) *paramSorter {
hs := ¶mSorter{
Keys: make([]string, 0, len(m)),
Values: make([]string, 0, len(m)),
}
for k, v := range m {
hs.Keys = append(hs.Keys, k)
hs.Values = append(hs.Values, v)
}
return hs
}
// 进行字典顺序排序 sort required method
func (hs *paramSorter) Sort() {
sort.Sort(hs)
}
// Additional function for function sort required method
func (hs *paramSorter) Len() int {
return len(hs.Values)
}
// Additional function for function sort required method
func (hs *paramSorter) Less(i, j int) bool {
return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
}
// Additional function for function paramsSorter.
func (hs *paramSorter) Swap(i, j int) {
hs.Values[i], hs.Values[j] = hs.Values[j], hs.Values[i]
hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
}
// 做签名处理
func doSign(params map[string]string, secretKey string) string {
hs := newParamsSorter(params)
// Sort the temp by the Ascending Order
hs.Sort()
// Get the CanonicalizedOSSHeaders
canonicalizedParams := ""
for i := range hs.Keys {
if i > 0 {
canonicalizedParams += "&"
}
canonicalizedParams += hs.Keys[i] + "=" + hs.Values[i]
}
signStr := canonicalizedParams
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(secretKey))
io.WriteString(h, signStr)
signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
return signedStr
}