-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsign.go
61 lines (56 loc) · 1.16 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
package tdmq
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"hash"
"net/url"
"sort"
"strings"
)
const (
HmacSHA1 = `HmacSHA1`
HmacSHA256 = `HmacSHA256`
)
func (c *Client) sign(host string, values url.Values) string {
plain := []string{c.Method}
if len(host) > 0 {
plain = append(plain, host)
}
if path := c.Url.Path; len(path) > 0 {
idx := strings.Index(path, `?`)
if idx >= 0 {
path = path[:idx]
}
plain = append(plain, path)
}
plain = append(plain, `?`)
var keys []string
for k := range values {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
k = strings.Replace(k, `_`, `.`, -1)
plain = append(plain, k, `=`, strings.Join(values[k], ``), `&`)
}
if plain[len(plain)-1] == `&` {
plain = plain[:len(plain)-1]
}
if c.Debug {
fmt.Println("String to sign:", strings.Join(plain, ``))
}
var h hash.Hash
if c.SignMethod == HmacSHA256 {
h = hmac.New(sha256.New, []byte(c.SecretKey))
} else {
h = hmac.New(sha1.New, []byte(c.SecretKey))
}
_, err := h.Write([]byte(strings.Join(plain, ``)))
if err != nil {
return ``
}
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}