/
crypto.go
273 lines (241 loc) · 6.14 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
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
package utils
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"errors"
"io"
)
// 获取md5
func Md5Encode(str string) string {
data := []byte(str)
md5Ctx := md5.New()
md5Ctx.Write(data)
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
// 获取sha1
func Sha1Encode(str string) string {
data := []byte(str)
_sha1 := sha1.New()
_sha1.Write(data)
return hex.EncodeToString(_sha1.Sum([]byte("")))
}
// 获取sha256
func Sha256Encode(str string) string {
sum256 := sha256.Sum256([]byte(str))
s := hex.EncodeToString(sum256[:])
return s
}
// RSA256 公钥密钥对生成
// @params: bits 密钥长度
// @returns: private 密钥
// @returns: public 公钥
func GenRsaKey(bits int) (e error, priKey string, pubKey string) {
// 生成私钥
privateKey, e := rsa.GenerateKey(rand.Reader, bits)
if e != nil {
return e, "", ""
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
priBlock := &pem.Block{
Type: "RAS PRIVATE KEY",
Bytes: derStream,
}
//fmt.Println("私密钥:",string(pem.EncodeToMemory(priBlock)))
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return e, "", ""
}
publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: derPkix,
}
//fmt.Printf("=======公钥文件内容=========%v", string(pem.EncodeToMemory(publicBlock)))
if err != nil {
return e, "", ""
}
return nil, string(pem.EncodeToMemory(priBlock)), string(pem.EncodeToMemory(publicBlock))
}
// Rsa256 加密
// @params: origData 原始数据
// @Params: pubKey 公钥
func RsaEncrypt(origData, pubKey []byte) ([]byte, error) {
//解密pem格式的公钥
block, _ := pem.Decode(pubKey)
if block == nil {
return nil, errors.New("public key error")
}
// 解析公钥
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// 类型断言
pub := pubInterface.(*rsa.PublicKey)
//加密
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
// Rsa256 加密简单
// @params: origData 原始数据
// @Params: pubKey 公钥
func RsaEncryptSimple(origData, pubKey string) (string, error) {
orgData := []byte(origData)
pubK := []byte(pubKey)
bytes, e := RsaEncrypt(orgData, pubK)
if e != nil {
return "", e
}
encode := Base64Encode(bytes)
return encode, nil
}
// Rsa256 解密
// @params: ciphertext 加密数据
// @Params: prvKey 私钥
func RsaDecrypt(ciphertext, privateKey []byte) ([]byte, error) {
//解密
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error!")
}
//解析PKCS1格式的私钥
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// 解密
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
// Rsa256 解密简单
func RsaDecryptSimple(ciphertext, privateKey string) (string, error) {
decode, i := Base64Decode(ciphertext)
if i != nil {
return "", i
}
pri := []byte(privateKey)
bytes, e := RsaDecrypt(decode, pri)
return string(bytes), e
}
// Rsa256 签名
// @params: origData 需要签名的数据
// @Params: prvKey 私钥
func RsaSign(data, prvKey []byte) ([]byte, error) {
hash := sha256.New()
hash.Write(data)
sum := hash.Sum(nil)
//解密私钥
block, _ := pem.Decode(prvKey)
if block == nil {
return nil, errors.New("private key error!")
}
//解析PKCS1格式的私钥
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// 签名
return rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA256, sum)
}
// Rsa256 验签
// @params: data 原始数据
// @params: signature 签名
// @params: publicKey 公钥
func RsaSignVer(data, signature, publicKey []byte) error {
hashed := sha256.Sum256(data)
block, _ := pem.Decode(publicKey)
if block == nil {
return errors.New("public key error")
}
// 解析公钥
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
// 类型断言
pub := pubInterface.(*rsa.PublicKey)
//验证签名
return rsa.VerifyPKCS1v15(pub, crypto.SHA256, hashed[:], signature)
}
// Rsa256 签名简单
// @params: origData 需要签名的数据
// @Params: prvKey 私钥
func RsaSignSimple(data, prvKey string) (string, error) {
bytes := []byte(data)
i := []byte(prvKey)
sign, e := RsaSign(bytes, i)
if e != nil {
return "", e
}
return Base64Encode(sign), nil
}
// Rsa256 验签简单
// @params: data 原始数据
// @params: signature 签名
// @params: publicKey 公钥
func RsaSignVerSimple(data, signature, publicKey string) error {
dat := []byte(data)
bytes, e := Base64Decode(signature)
if e != nil {
return e
}
pub := []byte(publicKey)
return RsaSignVer(dat, bytes, pub)
}
// Base64编码
func Base64Encode(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
// Base64解码
func Base64Decode(s string) ([]byte, error) {
return base64.StdEncoding.DecodeString(s)
}
// Base64URL编码
func Base64URLEncode(data []byte) string {
return base64.URLEncoding.EncodeToString(data)
}
// Base64URL解码
func Base64URLDecode(s string) ([]byte, error) {
return base64.URLEncoding.DecodeString(s)
}
// 对称加密 AES 高级标准加密
// Encrypt string to base64 crypto using AES
func AESEncrypt(key []byte, plaintext []byte) ([]byte, bool) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, false
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, false
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, true
}
// Decrypt from base64 to decrypted string
func AESDecrypt(key []byte, ciphertext []byte) ([]byte, bool) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, false
}
if len(ciphertext) < aes.BlockSize {
return nil, false
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext, true
}