forked from dwdwow/cex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_util.go
40 lines (32 loc) · 928 Bytes
/
sign_util.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
package cex
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
)
type Signer func(payload, key string) []byte
func SignByHmacSHA256(payload, key string) []byte {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(payload))
return h.Sum(nil)
}
func SignByHmacSHA512(payload, key string) []byte {
h := hmac.New(sha512.New, []byte(key))
h.Write([]byte(payload))
return h.Sum(nil)
}
func SignByHmacSHA256ToHex(payload, key string) string {
return hex.EncodeToString(SignByHmacSHA256(payload, key))
}
func SignByHmacSHA512ToHex(payload, key string) string {
return hex.EncodeToString(SignByHmacSHA512(payload, key))
}
func SignByHmacSHA256ToBase64(payload, key string) string {
return base64.StdEncoding.EncodeToString(SignByHmacSHA256(payload, key))
}
func Sha512ToHex(payload string) string {
res := sha512.Sum512([]byte(payload))
return hex.EncodeToString(res[:])
}