forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
274 lines (251 loc) · 7.54 KB
/
encrypt.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
package goutil
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"hash/fnv"
"io"
"github.com/henrylee2cn/ameda"
)
// Md5 returns the MD5 checksum string of the data.
func Md5(b []byte) string {
checksum := md5.Sum(b)
return hex.EncodeToString(checksum[:])
}
// Sha1 returns the sha1 checksum string of the data.
func Sha1(b []byte) string {
checksum := sha1.Sum(b)
return hex.EncodeToString(checksum[:])
}
// Sha256 returns the sha256 checksum string of the data.
func Sha256(b []byte) string {
checksum := sha256.Sum256(b)
return hex.EncodeToString(checksum[:])
}
// Sha512 returns the sha512 checksum string of the data.
func Sha512(b []byte) string {
checksum := sha512.Sum512(b)
return hex.EncodeToString(checksum[:])
}
// Fnv1aToUint64 returns the 64-bit FNV-1a hash sum of b.
func Fnv1aToUint64(b []byte) uint64 {
h := fnv.New64a()
h.Reset()
h.Write(b)
return h.Sum64()
}
// Fnv1aToUint32 returns the 32-bit FNV-1a hash sum of b.
func Fnv1aToUint32(b []byte) uint32 {
h := fnv.New32a()
h.Reset()
h.Write(b)
return h.Sum32()
}
// AESEncrypt uses ECB mode to encrypt a piece of data and then encodes it in hex.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESEncrypt(cipherkey, plaintext []byte, useBase64 ...bool) []byte {
block := mustNewCipher(cipherkey)
blockSize := block.BlockSize()
plaintext = pkcs5Padding(plaintext, blockSize)
r := make([]byte, len(plaintext))
dst := r
for len(plaintext) > 0 {
block.Encrypt(dst, plaintext)
plaintext = plaintext[blockSize:]
dst = dst[blockSize:]
}
return encode(r, useBase64)
}
// AESDecrypt hex decodes a piece of data and then decrypts it using ECB mode.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESDecrypt(cipherkey, ciphertext []byte, useBase64 ...bool) ([]byte, error) {
src, err := decode(ciphertext, useBase64)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(cipherkey)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
r := make([]byte, len(src))
dst := r
for len(src) > 0 {
block.Decrypt(dst, src)
src = src[blockSize:]
dst = dst[blockSize:]
}
return pkcs5Unpadding(r)
}
// AESCBCEncrypt uses CBC mode to encrypt a piece of data and then encodes it in hex.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESCBCEncrypt(cipherkey, plaintext []byte, useBase64 ...bool) []byte {
block := mustNewCipher(cipherkey)
blockSize := block.BlockSize()
plaintext = pkcs5Padding(plaintext, blockSize)
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
return encode(ciphertext, useBase64)
}
// AESCBCDecrypt hex decodes a piece of data and then decrypts it using CBC mode.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESCBCDecrypt(cipherkey, ciphertext []byte, useBase64 ...bool) ([]byte, error) {
ciphertext, err := decode(ciphertext, useBase64)
if err != nil {
return nil, err
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
// CBC mode always works in whole blocks.
if len(ciphertext)%aes.BlockSize != 0 {
return nil, errors.New("ciphertext is not a multiple of the block size")
}
block, err := aes.NewCipher(cipherkey)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, iv)
// CryptBlocks can work in-place if the two arguments are the same.
plaintext := ciphertext
mode.CryptBlocks(plaintext, ciphertext)
return pkcs5Unpadding(plaintext)
}
// AESCTREncrypt uses CTR mode to encrypt a piece of data and then encodes it in hex.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESCTREncrypt(cipherkey, plaintext []byte, useBase64 ...bool) []byte {
block := mustNewCipher(cipherkey)
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return encode(ciphertext, useBase64)
}
// AESCTRDecrypt hex decodes a piece of data and then decrypts it using CTR mode.
// The cipherkey argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func AESCTRDecrypt(cipherkey, ciphertext []byte, useBase64 ...bool) ([]byte, error) {
ciphertext, err := decode(ciphertext, useBase64)
if err != nil {
return nil, err
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
block, err := aes.NewCipher(cipherkey)
if err != nil {
return nil, err
}
stream := cipher.NewCTR(block, iv)
plaintext := ciphertext
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}
func mustNewCipher(cipherkey []byte) cipher.Block {
block, err := aes.NewCipher(cipherkey)
if err != nil {
panic(err)
}
return block
}
func pkcs5Padding(plaintext []byte, blockSize int) []byte {
n := byte(blockSize - len(plaintext)%blockSize)
for i := byte(0); i < n; i++ {
plaintext = append(plaintext, n)
}
return plaintext
}
func pkcs5Unpadding(r []byte) ([]byte, error) {
l := len(r)
if l == 0 {
return nil, errors.New("input padded bytes is empty")
}
last := int(r[l-1])
if l-last < 0 {
return nil, errors.New("input padded bytes is invalid")
}
n := byte(last)
pad := r[l-last : l]
isPad := true
for _, v := range pad {
if v != n {
isPad = false
break
}
}
if !isPad {
return nil, errors.New("remove pad error")
}
return r[:l-last], nil
}
func encode(src []byte, useBase64 []bool) []byte {
if ameda.OneBool(useBase64) {
return base64Encode(src)
}
return hexEncode(src)
}
func decode(src []byte, useBase64 []bool) ([]byte, error) {
if ameda.OneBool(useBase64) {
return base64Decode(src)
}
return hexDecode(src)
}
func hexEncode(src []byte) []byte {
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
return dst
}
func hexDecode(src []byte) ([]byte, error) {
dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
return dst, err
}
func base64Encode(src []byte) []byte {
buf := make([]byte, base64.RawURLEncoding.EncodedLen(len(src)))
base64.RawURLEncoding.Encode(buf, src)
return buf
}
func base64Decode(src []byte) ([]byte, error) {
dst := make([]byte, base64.RawURLEncoding.DecodedLen(len(src)))
n, err := base64.RawURLEncoding.Decode(dst, src)
return dst[:n], err
}