forked from Kucoin/kumex-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
63 lines (55 loc) · 1.38 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package kumex
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"time"
)
// Signer interface contains Sign() method.
type Signer interface {
Sign(plain []byte) []byte
}
// Sha256Signer is the sha256 Signer.
type Sha256Signer struct {
key []byte
}
// Sign makes a signature by sha256.
func (ss *Sha256Signer) Sign(plain []byte) []byte {
hm := hmac.New(sha256.New, ss.key)
hm.Write(plain)
return hm.Sum(nil)
}
// KcSigner is the implement of Signer for kumex.
type KcSigner struct {
Sha256Signer
apiKey string
apiSecret string
apiPassPhrase string
}
// Sign makes a signature by sha256 with `apiKey` `apiSecret` `apiPassPhrase`.
func (ks *KcSigner) Sign(plain []byte) []byte {
s := ks.Sha256Signer.Sign(plain)
return []byte(base64.StdEncoding.EncodeToString(s))
}
// Headers returns a map of signature header.
func (ks *KcSigner) Headers(plain string) map[string]string {
t := IntToString(time.Now().UnixNano() / 1000000)
p := []byte(t + plain)
s := string(ks.Sign(p))
return map[string]string{
"KC-API-KEY": ks.apiKey,
"KC-API-PASSPHRASE": ks.apiPassPhrase,
"KC-API-TIMESTAMP": t,
"KC-API-SIGN": s,
}
}
// NewKcSigner creates a instance of KcSigner.
func NewKcSigner(key, secret, passPhrase string) *KcSigner {
ks := &KcSigner{
apiKey: key,
apiSecret: secret,
apiPassPhrase: passPhrase,
}
ks.key = []byte(secret)
return ks
}