-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsignature.go
38 lines (32 loc) · 890 Bytes
/
signature.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
package agollo
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
neturl "net/url"
"strconv"
"time"
)
type signature struct {
AppID string `json:"appId"`
AccesskeySecret string `json:"accesskey_secret"`
}
func newSignature(appId, accesskeySecret string) *signature {
return &signature{AppID: appId, AccesskeySecret: accesskeySecret}
}
func (s *signature) getAuthorization(url, timestamp string) string {
sign := timestamp + signDelimiter + s.getURL2PathWithQuery(url)
key := []byte(s.AccesskeySecret)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(sign))
ss := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(ss)
}
func (s *signature) getTimestamp() string {
t := time.Now().UnixNano() / 1e6
return strconv.Itoa(int(t))
}
func (s *signature) getURL2PathWithQuery(rawurl string) string {
url, _ := neturl.Parse(rawurl)
return url.RequestURI()
}