-
Notifications
You must be signed in to change notification settings - Fork 4
/
gcm.go
289 lines (230 loc) · 5.99 KB
/
gcm.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package xcrypto
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"errors"
"github.com/fufuok/utils"
"github.com/fufuok/utils/base58"
)
const gcmStandardNonceSize = 12
// AesGCMEnStringHex 加密
func AesGCMEnStringHex(s string, key []byte) (string, []byte) {
return AesGCMEnHex(utils.S2B(s), key)
}
// AesGCMEnHex 加密
func AesGCMEnHex(b, key []byte) (string, []byte) {
res, nonce := AesGCMEncrypt(b, key)
return hex.EncodeToString(res), nonce
}
// AesGCMDeStringHex 解密
func AesGCMDeStringHex(s string, key, nonce []byte) string {
return utils.B2S(AesGCMDeHex(s, key, nonce))
}
// AesGCMDeHex 解密
func AesGCMDeHex(s string, key, nonce []byte) []byte {
if data, err := hex.DecodeString(s); err == nil {
return AesGCMDecrypt(data, key, nonce)
}
return nil
}
// AesGCMEnStringB58 加密
func AesGCMEnStringB58(s string, key []byte) (string, []byte) {
return AesGCMEnB58(utils.S2B(s), key)
}
// AesGCMEnB58 加密
func AesGCMEnB58(b, key []byte) (string, []byte) {
res, nonce := AesGCMEncrypt(b, key)
return base58.Encode(res), nonce
}
// AesGCMDeStringB58 解密
func AesGCMDeStringB58(s string, key, nonce []byte) string {
return utils.B2S(AesGCMDeB58(s, key, nonce))
}
// AesGCMDeB58 解密
func AesGCMDeB58(s string, key, nonce []byte) []byte {
return AesGCMDecrypt(base58.Decode(s), key, nonce)
}
// AesGCMEnStringB64 加密
func AesGCMEnStringB64(s string, key []byte) (string, []byte) {
return AesGCMEnB64(utils.S2B(s), key)
}
// AesGCMEnB64 加密
func AesGCMEnB64(b, key []byte) (string, []byte) {
res, nonce := AesGCMEncrypt(b, key)
return utils.B64UrlEncode(res), nonce
}
// AesGCMDeStringB64 解密
func AesGCMDeStringB64(s string, key, nonce []byte) string {
return utils.B2S(AesGCMDeB64(s, key, nonce))
}
// AesGCMDeB64 解密
func AesGCMDeB64(s string, key, nonce []byte) []byte {
return AesGCMDecrypt(utils.B64UrlDecode(s), key, nonce)
}
// AesGCMEncrypt AES-GCM 加密
func AesGCMEncrypt(plaintext, key []byte) (ciphertext []byte, nonce []byte) {
ciphertext, nonce, _ = AesGCMEncryptWithNonce(plaintext, key, nil, nil)
return
}
// AesGCMDecrypt AES-GCM 解密
func AesGCMDecrypt(ciphertext, key, nonce []byte) (plaintext []byte) {
plaintext, _ = AesGCMDecryptWithNonce(ciphertext, key, nonce, nil)
return
}
// AesGCMEncryptWithNonce AES-GCM 加密, (Galois/Counter Mode (GCM))
// key 长度分别是 16 (AES-128), 32 (AES-256)
func AesGCMEncryptWithNonce(plaintext, key, nonce, additionalData []byte) ([]byte, []byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
gcm, err := cipher.NewGCMWithNonceSize(block, gcmStandardNonceSize)
if err != nil {
return nil, nil, err
}
if len(nonce) == 0 {
nonce = utils.S2B(utils.RandString(gcmStandardNonceSize))
}
res := gcm.Seal(nil, nonce, plaintext, additionalData)
return res, nonce, nil
}
// AesGCMDecryptWithNonce AES-GCM 解密, (Galois/Counter Mode (GCM))
func AesGCMDecryptWithNonce(ciphertext, key, nonce, additionalData []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
res, err := gcm.Open(nil, nonce, ciphertext, additionalData)
if err != nil {
return nil, err
}
return res, nil
}
// GCMEnStringHex 加密
func GCMEnStringHex(s string, key []byte) string {
return GCMEnHex(utils.S2B(s), key)
}
// GCMEnHex 加密
func GCMEnHex(b, key []byte) string {
if res, err := GCMEncrypt(b, key); err == nil {
return hex.EncodeToString(res)
}
return ""
}
// GCMDeStringHex 解密
func GCMDeStringHex(s string, key []byte) string {
return utils.B2S(GCMDeHex(s, key))
}
// GCMDeHex 解密
func GCMDeHex(s string, key []byte) []byte {
if data, err := hex.DecodeString(s); err == nil {
if res, err := GCMDecrypt(data, key); err == nil {
return res
}
}
return nil
}
// GCMEnStringB58 加密
func GCMEnStringB58(s string, key []byte) string {
return GCMEnB58(utils.S2B(s), key)
}
// GCMEnB58 加密
func GCMEnB58(b, key []byte) string {
if res, err := GCMEncrypt(b, key); err == nil {
return base58.Encode(res)
}
return ""
}
// GCMDeStringB58 解密
func GCMDeStringB58(s string, key []byte) string {
return utils.B2S(GCMDeB58(s, key))
}
// GCMDeB58 解密
func GCMDeB58(s string, key []byte) []byte {
if res, err := GCMDecrypt(base58.Decode(s), key); err == nil {
return res
}
return nil
}
// GCMEnStringB64 加密
func GCMEnStringB64(s string, key []byte) string {
return GCMEnB64(utils.S2B(s), key)
}
// GCMEnB64 加密
func GCMEnB64(b, key []byte) string {
if res, err := GCMEncrypt(b, key); err == nil {
return utils.B64UrlEncode(res)
}
return ""
}
// GCMDeStringB64 解密
func GCMDeStringB64(s string, key []byte) string {
return utils.B2S(GCMDeB64(s, key))
}
// GCMDeB64 解密
func GCMDeB64(s string, key []byte) []byte {
if res, err := GCMDecrypt(utils.B64UrlDecode(s), key); err == nil {
return res
}
return nil
}
// GCMEncrypt AES-GCM 加密
func GCMEncrypt(plaintext, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := utils.RandBytes(gcm.NonceSize())
res := gcm.Seal(nonce, nonce, plaintext, nil)
return res, nil
}
// GCMDecrypt AES-GCM 解密
func GCMDecrypt(encrypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(encrypted) < nonceSize {
return nil, errors.New("encrypted value is not valid")
}
nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}