forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
113 lines (99 loc) · 2.47 KB
/
crypto.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
102
103
104
105
106
107
108
109
110
111
112
113
package crypto
import (
"crypto/hmac"
"crypto/md5" // nolint // Used by some exchanges
"crypto/rand"
"crypto/sha1" // nolint // Used by some exchanges
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"hash"
"io"
)
// Const declarations for common.go operations
const (
HashSHA1 = iota
HashSHA256
HashSHA512
HashSHA512_384
HashMD5
)
// HexEncodeToString takes in a hexadecimal byte array and returns a string
func HexEncodeToString(input []byte) string {
return hex.EncodeToString(input)
}
// Base64Decode takes in a Base64 string and returns a byte array and an error
func Base64Decode(input string) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return nil, err
}
return result, nil
}
// Base64Encode takes in a byte array then returns an encoded base64 string
func Base64Encode(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
// GetRandomSalt returns a random salt
func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {
if saltLen <= 0 {
return nil, errors.New("salt length is too small")
}
salt := make([]byte, saltLen)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, err
}
var result []byte
if input != nil {
result = input
}
result = append(result, salt...)
return result, nil
}
// GetMD5 returns a MD5 hash of a byte array
func GetMD5(input []byte) []byte {
m := md5.New() // nolint // Used by some exchanges
m.Write(input)
return m.Sum(nil)
}
// GetSHA512 returns a SHA512 hash of a byte array
func GetSHA512(input []byte) []byte {
sha := sha512.New()
sha.Write(input)
return sha.Sum(nil)
}
// GetSHA256 returns a SHA256 hash of a byte array
func GetSHA256(input []byte) []byte {
sha := sha256.New()
sha.Write(input)
return sha.Sum(nil)
}
// GetHMAC returns a keyed-hash message authentication code using the desired
// hashtype
func GetHMAC(hashType int, input, key []byte) []byte {
var hasher func() hash.Hash
switch hashType {
case HashSHA1:
hasher = sha1.New
case HashSHA256:
hasher = sha256.New
case HashSHA512:
hasher = sha512.New
case HashSHA512_384:
hasher = sha512.New384
case HashMD5:
hasher = md5.New
}
h := hmac.New(hasher, key)
h.Write(input)
return h.Sum(nil)
}
// Sha1ToHex takes a string, sha1 hashes it and return a hex string of the
// result
func Sha1ToHex(data string) string {
h := sha1.New() // nolint // Used by some exchanges
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}