fastEndecode.go
7.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package encrypt
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
"strings"
)
type KeySizeError int
func (k KeySizeError) Error() string {
return "fastEncryptDecode/fastEnDeCode: invalid key size " + strconv.Itoa(int(k)) + " | key size must be 16"
}
type ecbEncrypter ecb
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
// byte array to string
func ByteArr2Str(p []byte) string {
lp := len(p)
for i := 0; i < lp; i++ {
if p[i] == 0 {
return string(p[0:i])
}
}
return string(p)
}
func ByteArr2HexStr(bArr []byte) string {
buf := new(bytes.Buffer)
for _, b := range bArr {
s := strconv.FormatInt(int64(b&0xff), 16)
if len(s) == 1 {
buf.WriteString("0")
}
buf.WriteString(s)
}
return buf.String()
}
func Uint16ToBytes(n uint16) []byte {
return []byte{
byte(n),
byte(n >> 8),
}
}
func Uint32ToBytes(n uint32) []byte {
return []byte{
byte(n),
byte(n >> 8),
byte(n >> 16),
byte(n >> 24),
}
}
func Uint64ToBytes(n uint64) []byte {
return []byte{
byte(n),
byte(n >> 8),
byte(n >> 16),
byte(n >> 24),
byte(n >> 32),
byte(n >> 40),
byte(n >> 48),
byte(n >> 56),
}
}
func ByteArr2HexStrArr(bArr []byte) []string {
length := len(bArr)
slice := make([]string, length)
buf := new(bytes.Buffer)
for i := 0; i < length; i++ {
buf.Reset()
buf.WriteString("0x")
s := strconv.FormatInt(int64(bArr[i]&0xff), 16)
if len(s) == 1 {
buf.WriteString("0")
}
buf.WriteString(s)
slice[i] = buf.String()
}
return slice
}
func HexStr2ByteArr(hexString string) ([]byte, error) {
length := len(hexString) / 2
slice := make([]byte, length)
rs := []rune(hexString)
for i := 0; i < length; i++ {
s := string(rs[i*2 : i*2+2])
value, err := strconv.ParseInt(s, 16, 10)
if err != nil {
return nil, err
}
slice[i] = byte(value & 0xFF)
}
return slice, nil
}
func Utf82Unicode(code string) string {
cover := strconv.QuoteToASCII(code)
res := cover[1 : len(cover)-1]
return res
}
func Unicode2Utf8(code string) string {
unicodeTemp := strings.Split(code, "\\u")
var context string
for _, v := range unicodeTemp {
if len(v) < 1 {
continue
}
temp, err := strconv.ParseInt(v, 16, 32)
if err != nil {
panic(err)
}
context += fmt.Sprintf("%c", temp)
}
return context
}
//string to md5
func String2MD5(code string) string {
h := md5.New()
h.Write([]byte(code))
rs := hex.EncodeToString(h.Sum(nil))
return rs
}
func MD5Verify(code string, md5Str string) bool {
return 0 == strings.Compare(String2MD5(code), md5Str)
}
//MD5 hash
func MD5hash(code []byte) string {
h := md5.New()
h.Write(code)
rs := hex.EncodeToString(h.Sum(nil))
return rs
}
func checkKeySize(key []byte) error {
len := len(key)
if len != 16 {
return KeySizeError(len)
}
return nil
}
// AES encrypt pkcs7padding CBC, key for choose algorithm
func AES_CBC_PKCS7_Encrypt(plantText, key string) (string, error) {
res, err := AES_CBC_PKCS7_EncryptByte([]byte(plantText), []byte(key))
return ByteArr2Str(res), err
}
// AES encrypt pkcs7padding CBC, key for choose algorithm
func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) {
err := checkKeySize(key)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plantText = pKCS7Padding(plantText, block.BlockSize())
blockModel := cipher.NewCBCEncrypter(block, key)
cipherText := make([]byte, len(plantText))
blockModel.CryptBlocks(cipherText, plantText)
return cipherText, nil
}
func AES_CBC_PKCS7_Decrypt(cipherText, key string) (string, error) {
result, err := AES_CBC_PKCS7_DecryptByte([]byte(cipherText), []byte(key))
str := ByteArr2Str(result)
return str, err
}
func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) {
err := checkKeySize(key)
if err != nil {
return nil, err
}
keyBytes := []byte(key)
block, err := aes.NewCipher(keyBytes)
if err != nil {
return nil, err
}
blockModel := cipher.NewCBCDecrypter(block, keyBytes)
plantText := make([]byte, len(cipherText))
blockModel.CryptBlocks(plantText, cipherText)
plantText = pKCS7UnPadding(plantText, block.BlockSize())
return plantText, nil
}
//AES Decrypt pkcs7padding CBC, key for choose algorithm
func pKCS7UnPadding(plantText []byte, blockSize int) []byte {
length := len(plantText)
unPadding := int(plantText[length-1])
return plantText[:(length - unPadding)]
}
func pKCS7Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func AES_ECB_PKCS5_Encrypt(cipherText, key string) (string, error) {
result, err := AES_ECB_PKCS5_EncryptByte([]byte(cipherText), []byte(key))
str := ByteArr2Str(result)
return str, err
}
func AES_ECB_PKCS5_EncryptByte(cipherText, key []byte) ([]byte, error) {
err := checkKeySize(key)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ecb := newECBEncrypter(block)
content := cipherText
content = PKCS5Padding(content, block.BlockSize())
crypted := make([]byte, len(content))
ecb.CryptBlocks(crypted, content)
// 普通base64编码加密 区别于urlsafe base64
return crypted, nil
}
func AES_ECB_PKCS5_Decrypt(cipherText, key string) (string, error) {
result, err := AES_ECB_PKCS5_DecryptByte([]byte(cipherText), []byte(key))
str := ByteArr2Str(result)
return str, err
}
func AES_ECB_PKCS5_DecryptByte(cipherText, key []byte) ([]byte, error) {
err := checkKeySize(key)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := newECBDecrypter(block)
origData := make([]byte, len(cipherText))
blockMode.CryptBlocks(origData, cipherText)
origData = PKCS5UnPadding(origData)
return origData, nil
}
func Base64UrlSafeEncode(source []byte) string {
// Base64 Url Safe is the same as Base64 but does not contain '/' and '+' (replaced by '_' and '-') and trailing '=' are removed.
bytearr := base64.StdEncoding.EncodeToString(source)
safeurl := strings.Replace(string(bytearr), "/", "_", -1)
safeurl = strings.Replace(safeurl, "+", "-", -1)
safeurl = strings.Replace(safeurl, "=", "", -1)
return safeurl
}
func PKCS5Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
// 去掉最后一个字节 unpadding 次
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
// mode, using the given Block.
func newECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int {
return x.blockSize
}
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecrypter ecb
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
// mode, using the given Block.
func newECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int {
return x.blockSize
}
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}