-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsign.go
35 lines (27 loc) · 778 Bytes
/
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
package lunar
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"time"
)
const (
AuthorizationFormat = "Apollo %s:%s"
Delimiter = "\n"
)
func sign(timestamp, pathWithQuery, secret string) string {
stringToSign := timestamp + Delimiter + pathWithQuery
h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(stringToSign))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func buildHeaders(pathWithQuery, appID, secret string) map[string]string {
ms := time.Now().UnixNano() / int64(time.Millisecond)
timestamp := fmt.Sprintf("%d", ms)
signature := sign(timestamp, pathWithQuery, secret)
m := make(map[string]string)
m["Authorization"] = fmt.Sprintf(AuthorizationFormat, appID, signature)
m["Timestamp"] = timestamp
return m
}