-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
aliyun_signer.go
97 lines (83 loc) · 1.83 KB
/
aliyun_signer.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
93
94
95
96
97
package util
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"hash"
"io"
"net/url"
)
// https://github.com/rosbit/aliyun-sign/blob/master/aliyun-sign.go
var (
signMethodMap = map[string]func() hash.Hash{
"HMAC-SHA1": sha1.New,
"HMAC-SHA256": sha256.New,
"HMAC-MD5": md5.New,
}
)
func HmacSign(signMethod string, httpMethod string, appKeySecret string, vals url.Values) (signature []byte) {
key := []byte(appKeySecret + "&")
var h hash.Hash
if method, ok := signMethodMap[signMethod]; ok {
h = hmac.New(method, key)
} else {
h = hmac.New(sha1.New, key)
}
makeDataToSign(h, httpMethod, vals)
return h.Sum(nil)
}
func HmacSignToB64(signMethod string, httpMethod string, appKeySecret string, vals url.Values) (signature string) {
return base64.StdEncoding.EncodeToString(HmacSign(signMethod, httpMethod, appKeySecret, vals))
}
type strToEnc struct {
s string
e bool
}
func makeDataToSign(w io.Writer, httpMethod string, vals url.Values) {
in := make(chan *strToEnc)
go func() {
in <- &strToEnc{s: httpMethod}
in <- &strToEnc{s: "&"}
in <- &strToEnc{s: "/", e: true}
in <- &strToEnc{s: "&"}
in <- &strToEnc{s: vals.Encode(), e: true}
close(in)
}()
specialUrlEncode(in, w)
}
var (
encTilde = "%7E" // '~' -> "%7E"
encBlank = []byte("%20") // ' ' -> "%20"
tilde = []byte("~")
)
func specialUrlEncode(in <-chan *strToEnc, w io.Writer) {
for s := range in {
if !s.e {
io.WriteString(w, s.s)
continue
}
l := len(s.s)
for i := 0; i < l; {
ch := s.s[i]
switch ch {
case '%':
if encTilde == s.s[i:i+3] {
w.Write(tilde)
i += 3
continue
}
fallthrough
case '*', '/', '&', '=':
fmt.Fprintf(w, "%%%02X", ch)
case '+':
w.Write(encBlank)
default:
fmt.Fprintf(w, "%c", ch)
}
i += 1
}
}
}