-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_sign.go
43 lines (37 loc) · 1003 Bytes
/
api_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
package hook
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"sort"
"strings"
)
// APISign Create signature for sls api
func APISign(secret string, method string, headers map[string]string, resource string) string {
var contentMD5, contentType, date string
if v, exist := headers[HeaderContentMd5]; exist {
contentMD5 = v
}
if v, exist := headers[HeaderContentType]; exist {
contentType = v
}
if v, exist := headers[HeaderDate]; exist {
date = v
}
logHeaders := make([]string, 0)
for k, v := range headers {
if strings.HasPrefix(k, "x-log") || strings.HasPrefix(k, "x-acs") {
logHeaders = append(logHeaders, k+":"+strings.TrimSpace(v))
}
}
sort.Strings(logHeaders)
stringToSign := method + "\n" +
contentMD5 + "\n" +
contentType + "\n" +
date + "\n" +
strings.Join(logHeaders, "\n") + "\n" +
resource
sha1Hash := hmac.New(sha1.New, []byte(secret))
sha1Hash.Write([]byte(stringToSign))
return base64.StdEncoding.EncodeToString(sha1Hash.Sum(nil))
}