forked from Kucoin/kucoin-futures-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
89 lines (77 loc) · 2.06 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
apiKeyVersion 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))
ksHeaders := map[string]string{
"KC-API-KEY": ks.apiKey,
"KC-API-PASSPHRASE": ks.apiPassPhrase,
"KC-API-TIMESTAMP": t,
"KC-API-SIGN": s,
}
if ks.apiKeyVersion != "" && ks.apiKeyVersion != ApiKeyVersionV1 {
ksHeaders["KC-API-KEY-VERSION"] = ks.apiKeyVersion
}
return ksHeaders
}
// 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
}
// NewKcSignerV2 creates a instance of KcSigner.
func NewKcSignerV2(key, secret, passPhrase string) *KcSigner {
ks := &KcSigner{
apiKey: key,
apiSecret: secret,
apiPassPhrase: passPhraseEncrypt([]byte(secret), []byte(passPhrase)),
apiKeyVersion: ApiKeyVersionV2,
}
ks.key = []byte(secret)
return ks
}
// passPhraseEncrypt, encrypt passPhrase
func passPhraseEncrypt(key, plain []byte) string {
hm := hmac.New(sha256.New, key)
hm.Write(plain)
return base64.StdEncoding.EncodeToString(hm.Sum(nil))
}