-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrypto.go
160 lines (142 loc) · 3.81 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
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
package xutil
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
)
//================================================================================================
//Hash Hash
type Hash struct{ Data string }
//NewHash NewHash
func NewHash(dat string) Hash {
return Hash{Data: dat}
}
//MD5 MD5
func (h Hash) MD5() string {
c := md5.New()
c.Write([]byte(h.Data))
return hex.EncodeToString(c.Sum(nil))
}
//SHA1 SHA1
func (h Hash) SHA1() string {
c := sha1.New()
c.Write([]byte(h.Data))
return hex.EncodeToString(c.Sum(nil))
}
//================================================================================================
//明文补码算法
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 := int(origData[length-1])
return origData[:(length - unpadding)]
}
//明文补码算法: 用0去填充
func zeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
//明文减码算法:用0去填充
func zeroUnPadding(origData []byte) []byte {
return bytes.TrimFunc(origData,
func(r rune) bool {
return r == rune(0)
})
}
//================================================================================================
// var modes = []string{"ECB", "CBC", "CTR", "OFB", "CFB"}
// var paddings = []string{"PKCS5", "ZERO"}
type Crypto struct {
Key, IV []byte
Algorithm, Mode, Padding string
}
func NewCrypto(key []byte) Crypto {
return Crypto{Key: key, IV: key, Algorithm: "AES", Mode: "CBC", Padding: "PKCS5"}
}
func (c *Crypto) padding(data []byte, blockSize int) []byte {
if c.Padding == "PKCS5" {
return pKCS5Padding(data, blockSize)
}
return zeroPadding(data, blockSize)
}
func (c *Crypto) unpadding(data []byte) []byte {
if c.Padding == "PKCS5" {
return pKCS5UnPadding(data)
}
return zeroUnPadding(data)
}
//================================================================================================
func (c *Crypto) Encrypt(data []byte) (dst []byte, err error) {
var block cipher.Block
if c.Algorithm == "AES" {
block, err = aes.NewCipher(c.Key)
} else if c.Algorithm == "DES" {
block, err = des.NewCipher(c.Key)
}
if err != nil {
return nil, err
}
bs := block.BlockSize()
data = c.padding(data, bs)
dst = make([]byte, len(data))
switch c.Mode {
case "ECB":
for len(data) > 0 {
block.Encrypt(dst, data[:bs])
data = data[bs:]
dst = dst[bs:]
}
case "CBC":
cipher.NewCBCEncrypter(block, c.IV).CryptBlocks(dst, data)
case "CTR":
cipher.NewCTR(block, c.IV).XORKeyStream(dst, data)
case "OFB":
cipher.NewOFB(block, c.IV).XORKeyStream(dst, data)
case "CFB":
cipher.NewCFBEncrypter(block, c.IV).XORKeyStream(dst, data)
}
return dst, nil
}
func (c *Crypto) Decrypt(data []byte) (dst []byte, err error) {
var block cipher.Block
if c.Algorithm == "AES" {
block, err = aes.NewCipher(c.Key)
} else if c.Algorithm == "DES" {
block, err = des.NewCipher(c.Key)
}
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
dst = make([]byte, len(data))
bs := block.BlockSize()
switch c.Mode {
case "ECB":
for len(data) > 0 {
block.Decrypt(dst, data[:bs])
data = data[bs:]
dst = dst[bs:]
}
case "CBC":
cipher.NewCBCDecrypter(block, c.IV).CryptBlocks(dst, data)
case "CTR":
cipher.NewCTR(block, c.IV).XORKeyStream(dst, data)
case "OFB":
cipher.NewOFB(block, c.IV).XORKeyStream(dst, data)
case "CFB":
cipher.NewCFBDecrypter(block, c.IV).XORKeyStream(dst, data)
}
return c.unpadding(dst), nil
}