forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ParamSignUtils.go
101 lines (87 loc) · 2.31 KB
/
ParamSignUtils.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
90
91
92
93
94
95
96
97
98
99
100
101
package goex
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
)
/**
*md5签名,okcoin和huobi适用
*/
func GetParamMD5Sign(secret, params string) (string, error) {
hash := md5.New()
_, err := hash.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func GetSHA(text string) (string, error) {
sha := sha1.New()
_, err := sha.Write([]byte(text))
if err != nil {
return "", err
}
return hex.EncodeToString(sha.Sum(nil)), nil
}
func GetParamHmacSHA256Sign(secret, params string) (string, error) {
mac := hmac.New(sha256.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func GetParamHmacSHA512Sign(secret, params string) (string, error) {
mac := hmac.New(sha512.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func GetParamHmacSHA1Sign(secret, params string) (string, error) {
mac := hmac.New(sha1.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func GetParamHmacMD5Sign(secret, params string) (string, error) {
mac := hmac.New(md5.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
/*for bitfinex.com*/
func GetParamHmacSha384Sign(secret, params string) (string, error) {
mac := hmac.New(sha512.New384, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", nil
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func GetParamHmacSHA256Base64Sign(secret, params string) (string, error) {
mac := hmac.New(sha256.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
signByte := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(signByte), nil
}
func GetParamHmacSHA512Base64Sign(hmac_key string, hmac_data string) string {
hmh := hmac.New(sha512.New, []byte(hmac_key))
hmh.Write([]byte(hmac_data))
hex_data := hex.EncodeToString(hmh.Sum(nil))
hash_hmac_bytes := []byte(hex_data)
hmh.Reset()
return base64.StdEncoding.EncodeToString(hash_hmac_bytes)
}