-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign.go
64 lines (54 loc) · 1.79 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
package sns
import (
"crypto/hmac"
"encoding/base64"
"launchpad.net/goamz/aws"
"sort"
"strings"
)
var b64 = base64.StdEncoding
/*
func sign(auth aws.Auth, method, path string, params url.Values, headers http.Header) {
var host string
for k, v := range headers {
k = strings.ToLower(k)
switch k {
case "host":
host = v[0]
}
}
params["AWSAccessKeyId"] = []string{auth.AccessKey}
params["SignatureVersion"] = []string{"2"}
params["SignatureMethod"] = []string{"HmacSHA256"}
var sarry []string
for k, v := range params {
sarry = append(sarry, aws.Encode(k) + "=" + aws.Encode(v[0]))
}
sort.StringSlice(sarry).Sort()
joined := strings.Join(sarry, "&")
payload := strings.Join([]string{method, host, "/", joined}, "\n")
hash := hmac.NewSHA256([]byte(auth.SecretKey))
hash.Write([]byte(payload))
signature := make([]byte, b64.EncodedLen(hash.Size()))
b64.Encode(signature, hash.Sum())
params["Signature"] = []string{"AWS " + string(signature)}
println("Payload:", payload)
println("Signature:", strings.Join(params["Signature"], "|"))
}*/
func sign(auth aws.Auth, method, path string, params map[string]string, host string) {
params["AWSAccessKeyId"] = auth.AccessKey
params["SignatureVersion"] = "2"
params["SignatureMethod"] = "HmacSHA256"
var sarray []string
for k, v := range params {
sarray = append(sarray, aws.Encode(k)+"="+aws.Encode(v))
}
sort.StringSlice(sarray).Sort()
joined := strings.Join(sarray, "&")
payload := method + "\n" + host + "\n" + path + "\n" + joined
hash := hmac.NewSHA256([]byte(auth.SecretKey))
hash.Write([]byte(payload))
signature := make([]byte, b64.EncodedLen(hash.Size()))
b64.Encode(signature, hash.Sum())
params["Signature"] = string(signature)
}