-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
97 lines (88 loc) · 2.61 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
93
94
95
96
97
package main
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"sort"
"strings"
)
const AcceptHeader = "Accept"
const ContentTypeHeader = "Content-Type"
const AuthorizationHeader = "Authorization"
const JsonType = "application/json"
const BiliVersion = "1.0"
const HmacSha256 = "HMAC-SHA256"
const BiliTimestampHeader = "x-bili-timestamp"
const BiliSignatureMethodHeader = "x-bili-signature-method"
const BiliSignatureNonceHeader = "x-bili-signature-nonce"
const BiliAccessKeyIdHeader = "x-bili-accesskeyid"
const BiliSignVersionHeader = "x-bili-signature-version"
const BiliContentMD5Header = "x-bili-content-md5"
//CreateSignature 生成Authorization加密串
func CreateSignature(header *CommonHeader, accessKeySecret string) string {
sStr := header.ToSortedString()
return HmacSHA256(accessKeySecret, sStr)
}
func Md5(str string) (md5str string) {
data := []byte(str)
has := md5.Sum(data)
md5str = fmt.Sprintf("%x", has)
return md5str
}
func HmacSHA256(key string, data string) string {
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(data))
return hex.EncodeToString(mac.Sum(nil))
}
type CommonHeader struct {
ContentType string
ContentAcceptType string
Timestamp string
SignatureMethod string
SignatureVersion string
Authorization string
Nonce string
AccessKeyId string
ContentMD5 string
}
// ToMap 所有字段转map<string, string>
func (h *CommonHeader) ToMap() map[string]string {
return map[string]string{
BiliTimestampHeader: h.Timestamp,
BiliSignatureMethodHeader: h.SignatureMethod,
BiliSignatureNonceHeader: h.Nonce,
BiliAccessKeyIdHeader: h.AccessKeyId,
BiliSignVersionHeader: h.SignatureVersion,
BiliContentMD5Header: h.ContentMD5,
AuthorizationHeader: h.Authorization,
ContentTypeHeader: h.ContentType,
AcceptHeader: h.ContentAcceptType,
}
}
// ToSortMap 参与加密的字段转map<string, string>
func (h *CommonHeader) ToSortMap() map[string]string {
return map[string]string{
BiliTimestampHeader: h.Timestamp,
BiliSignatureMethodHeader: h.SignatureMethod,
BiliSignatureNonceHeader: h.Nonce,
BiliAccessKeyIdHeader: h.AccessKeyId,
BiliSignVersionHeader: h.SignatureVersion,
BiliContentMD5Header: h.ContentMD5,
}
}
//ToSortedString 生成需要加密的文本
func (h *CommonHeader) ToSortedString() (sign string) {
hMap := h.ToSortMap()
var hSil []string
for k := range hMap {
hSil = append(hSil, k)
}
sort.Strings(hSil)
for _, v := range hSil {
sign += v + ":" + hMap[v] + "\n"
}
sign = strings.TrimRight(sign, "\n")
return
}