forked from hbdmapi/huobi_futures_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
41 lines (34 loc) · 790 Bytes
/
signer.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
package reqbuilder
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"strings"
)
type Signer struct {
key []byte
}
func (p *Signer) Init(key string) *Signer {
p.key = []byte(key)
return p
}
func (p *Signer) Sign(method string, host string, path string, parameters string) string {
if method == "" || host == "" || path == "" || parameters == "" {
return ""
}
var sb strings.Builder
sb.WriteString(method)
sb.WriteString("\n")
sb.WriteString(host)
sb.WriteString("\n")
sb.WriteString(path)
sb.WriteString("\n")
sb.WriteString(parameters)
return p.sign(sb.String())
}
func (p *Signer) sign(payload string) string {
hash := hmac.New(sha256.New, p.key)
hash.Write([]byte(payload))
result := base64.StdEncoding.EncodeToString(hash.Sum(nil))
return result
}