-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto_driver.go
217 lines (185 loc) · 4.79 KB
/
crypto_driver.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"hash"
"io"
"path/filepath"
"time"
"github.com/gomig/utils"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/sha3"
)
// cryptoDriver cryptography driver
type cryptoDriver struct {
key string
}
func (cryptoDriver) err(format string, args ...any) error {
return utils.TaggedError([]string{"Crypto"}, format, args...)
}
// Hash make hash for data
func (cd cryptoDriver) Hash(data string, algo HashAlgo) (string, error) {
var hasher hash.Hash
key := []byte(cd.key)
switch algo {
case MD4:
hasher = hmac.New(md4.New, key)
case MD5:
hasher = hmac.New(md5.New, key)
case SHA1:
hasher = hmac.New(sha1.New, key)
case SHA256:
hasher = hmac.New(sha256.New, key)
case SHA256224:
hasher = hmac.New(sha256.New224, key)
case SHA512:
hasher = hmac.New(sha512.New, key)
case SHA512224:
hasher = hmac.New(sha512.New512_224, key)
case SHA512256:
hasher = hmac.New(sha512.New512_256, key)
case SHA384:
hasher = hmac.New(sha512.New384, key)
case SHA3224:
hasher = hmac.New(sha3.New224, key)
case SHA3256:
hasher = hmac.New(sha3.New256, key)
case SHA3384:
hasher = hmac.New(sha3.New384, key)
case SHA3512:
hasher = hmac.New(sha3.New512, key)
case KECCAK256:
hasher = hmac.New(sha3.NewLegacyKeccak256, key)
case KECCAK512:
hasher = hmac.New(sha3.NewLegacyKeccak512, key)
}
if hasher == nil {
return "", cd.err("invalid hasher %s.", algo)
}
_, err := hasher.Write([]byte(data))
if err != nil {
return "", cd.err(err.Error())
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// HashFilename make hashed filename based on current timestamp
func (cd cryptoDriver) HashFilename(filename string, algo HashAlgo) (string, error) {
ext := filepath.Ext(filename)
res, err := cd.Hash(fmt.Sprintf("%s-at-%d", filename, time.Now().Nanosecond()), algo)
if err != nil {
return "", cd.err(err.Error())
}
return res + ext, nil
}
// HashSize get hash size for algorithm
// return -1 if invalid algo passed
func (cd cryptoDriver) HashSize(algo HashAlgo) int {
h, err := cd.Hash("Test", algo)
if err != nil {
return -1
}
return len(h)
}
// Check check data against hash
func (cd cryptoDriver) Check(data string, hash string, algo HashAlgo) (bool, error) {
res, err := cd.Hash(data, algo)
if err != nil {
return false, cd.err(err.Error())
}
return res == hash, nil
}
// Encrypt data
func (cd cryptoDriver) Encrypt(data []byte) ([]byte, error) {
var err error
// generate key md5
key, err := cd.Hash(cd.key, MD5)
if err != nil {
return nil, cd.err(err.Error())
}
// generate cipher
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, cd.err(err.Error())
}
// generate gcm
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, cd.err(err.Error())
}
// generate nonce
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, cd.err(err.Error())
}
// encrypt data
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext, nil
}
// Decrypt data
func (cd cryptoDriver) Decrypt(data []byte) ([]byte, error) {
var err error
// generate key md5
key, err := cd.Hash(cd.key, MD5)
if err != nil {
return nil, cd.err(err.Error())
}
// generate cipher
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, cd.err(err.Error())
}
// generate gcm
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, cd.err(err.Error())
}
// generate nonce
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, cd.err(err.Error())
}
return plaintext, nil
}
// EncryptHEX encrypt data and return encrypted value as hex encoded string
func (cd cryptoDriver) EncryptHEX(data []byte) (string, error) {
res, err := cd.Encrypt(data)
if err != nil {
return "", err
}
return hex.EncodeToString(res), nil
}
// DecryptHex decrypt data from hex encoded string.
func (cd cryptoDriver) DecryptHex(hexString string) ([]byte, error) {
data, err := hex.DecodeString(hexString)
if err != nil {
return nil, cd.err(err.Error())
}
return cd.Decrypt(data)
}
// EncryptBase64 encrypt data and return encrypted value as base64 encoded string
func (cd cryptoDriver) EncryptBase64(data []byte) (string, error) {
res, err := cd.Encrypt(data)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(res), nil
}
// DecryptBase64 decrypt data from base64 encoded string.
func (cd cryptoDriver) DecryptBase64(base64String string) ([]byte, error) {
data, err := base64.URLEncoding.DecodeString(base64String)
if err != nil {
return nil, cd.err(err.Error())
}
return cd.Decrypt(data)
}