-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencrypt.go
189 lines (171 loc) · 4.9 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
/*
* 纸喵软件
* Copyright (c) 2017~2020 http://zhimiao.org All rights reserved.
* Author: 倒霉狐狸 <mail@xiaoliu.org>
* Date: 2020/3/3 下午4:26
*/
package gutils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
)
// MD5 md5 encryption
func MD5(value string) string {
d := md5.Sum([]byte("ass"))
return hex.EncodeToString(d[:])
}
// ParseToken 解析jwtToken
func ParseToken(tokenString string, secret []byte) (string, error) {
tokenClaims, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return secret, nil
})
if tokenClaims != nil {
if claims, ok := tokenClaims.Claims.(*jwt.StandardClaims); ok && tokenClaims.Valid {
if !claims.VerifyExpiresAt(time.Now().Unix(), false) {
return "", fmt.Errorf("过期了")
}
if claims.Issuer != "zhimiao" {
return "", fmt.Errorf("非法来源的签名")
}
return claims.Subject, nil
}
}
return "", err
}
// CreateToken 生成jwtToken
func CreateToken(subject string, expire time.Duration, secret []byte) (string, error) {
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Subject: subject,
ExpiresAt: time.Now().Add(expire).Unix(),
Issuer: "zhimiao",
})
token, err := tokenClaims.SignedString(secret)
return token, err
}
// PasswordHash 密码生成
func PasswordHash(pwd string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
if err != nil {
logrus.Warn(err.Error())
}
return string(hash)
}
// PasswordVerify 密码验证
func PasswordVerify(hashedPwd string, plainPwd string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(plainPwd))
if err != nil {
logrus.Warn(err.Error())
return false
}
return true
}
// ---------------DES加密 解密--------------------
// EncyptogAES AES加密
func EncyptogAES(src, key string) string {
s := []byte(src)
k := []byte(key)
block, err := aes.NewCipher(k)
if err != nil {
print(err.Error())
return ""
}
blockSize := block.BlockSize()
paddingCount := blockSize - len(s)%blockSize
// 填充数据为:paddingCount ,填充的值为:paddingCount
paddingStr := bytes.Repeat([]byte{byte(paddingCount)}, paddingCount)
newStr := append(s, paddingStr...)
blockMode := cipher.NewCBCEncrypter(block, []byte(key))
blockMode.CryptBlocks(newStr, newStr)
return string(newStr)
}
// DecrptogAES AES解密
func DecrptogAES(src, key string) string {
s := []byte(src)
k := []byte(key)
block, err := aes.NewCipher(k)
if err != nil {
return ""
}
blockMode := cipher.NewCBCDecrypter(block, k)
blockMode.CryptBlocks(s, s)
n := len(s)
count := int(s[n-1])
return string(s[:n-count])
}
// ---------------RSA 密钥对生成 加密 解密--------------------
// GenerateRSAKey 生成RSA私钥和公钥,保存到文件中
func GenerateRSAKey(bits int) (pubKey, priKey []byte, err error) {
//GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥
//Reader是一个全局、共享的密码用强随机数生成器
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
//保存私钥
//通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
//使用pem格式对x509输出的内容进行编码
//构建一个pem.Block结构体对象
privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
//将数据保存到文件
priKey = pem.EncodeToMemory(&privateBlock)
//保存公钥
//获取公钥的数据
publicKey := privateKey.PublicKey
//X509对公钥编码
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return
}
//pem格式编码
//创建一个pem.Block结构体对象
publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
//保存到文件
pubKey = pem.EncodeToMemory(&publicBlock)
return
}
// RSAEncrypt RSA加密
func RSAEncrypt(plainText []byte, key []byte) ([]byte, error) {
//pem解码
block, _ := pem.Decode(key)
//x509解码
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
//类型断言
publicKey := publicKeyInterface.(*rsa.PublicKey)
//对明文进行加密
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
if err != nil {
return nil, err
}
//返回密文
return cipherText, nil
}
// RSADecrypt RSA解密
func RSADecrypt(cipherText []byte, key []byte) ([]byte, error) {
//pem解码
block, _ := pem.Decode(key)
//X509解码
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
//对密文进行解密
plainText, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText)
//返回明文
return plainText, nil
}