generated from liuzhaomax/go-maxms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptox.go
214 lines (184 loc) · 5.2 KB
/
cryptox.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
package core
import (
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"github.com/anaskhan96/go-password-encoder"
)
func MD5(byt []byte) string {
hash := md5.New()
_, _ = hash.Write(byt)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func MD5Str(str string) string {
return MD5([]byte(str))
}
func SHA1(byt []byte) string {
hash := sha1.New()
_, _ = hash.Write(byt)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func SHA1Str(str string) string {
return SHA1([]byte(str))
}
func SHA1MD5Str(str string) string {
return SHA1Str(MD5Str(str))
}
func SHA256(byt []byte) string {
hash := sha256.New()
hash.Write(byt)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func SHA256Str(str string) string {
return SHA256([]byte(str))
}
func SHA256MD5Str(str string) string {
return SHA256Str(MD5Str(str))
}
func HmacSHA256(byt []byte, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write(byt)
return hex.EncodeToString(h.Sum(nil))
}
func HmacSHA256Str(str string, secret string) string {
return HmacSHA256([]byte(str), secret)
}
func BASE64Encode(byt []byte) string {
encoded := base64.StdEncoding.EncodeToString(byt)
return encoded
}
func BASE64EncodeStr(str string) string {
encoded := BASE64Encode([]byte(str))
return encoded
}
func BASE64Decode(str string) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(str)
return decoded, err
}
func BASE64DecodeStr(str string) (string, error) {
decoded, err := BASE64Decode(str)
if err == nil {
decodedStr := string(decoded)
return decodedStr, nil
}
return "", err
}
// RSA加密
func GenRSAKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
publicKey := &privateKey.PublicKey
return privateKey, publicKey, err
}
func PublicKeyToString(publicKey *rsa.PublicKey) (string, error) {
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return "", err
}
publicKeyPem := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
})
publicKeyStr := base64.StdEncoding.EncodeToString(publicKeyPem)
return publicKeyStr, err
}
func PrivateKeyToString(privateKey *rsa.PrivateKey) (string, error) {
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyPem := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
})
privateKeyStr := base64.StdEncoding.EncodeToString(privateKeyPem)
return privateKeyStr, nil
}
func PublicKeyB64StrToStruct(publicKeyStr string) (*rsa.PublicKey, error) {
publicKeyPem, err := base64.StdEncoding.DecodeString(publicKeyStr)
if err != nil {
return nil, fmt.Errorf("failed to decode PEM block in base64 format containing the private key")
}
block, _ := pem.Decode(publicKeyPem)
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the public key")
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
rsaPublicKey, ok := key.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("parsed key is not an RSA public key")
}
return rsaPublicKey, nil
}
func PrivateKeyB64StrToStruct(privateKeyStr string) (*rsa.PrivateKey, error) {
privateKeyPem, err := base64.StdEncoding.DecodeString(privateKeyStr)
if err != nil {
return nil, fmt.Errorf("failed to decode PEM block in base64 format containing the private key")
}
block, _ := pem.Decode(privateKeyPem)
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the private key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// x509.ParsePKCS1PrivateKey 返回的是 *rsa.PrivateKey 类型,
// 而不是一个通用的 crypto.PrivateKey,
// 所以在类型断言时不需要使用 .(*rsa.PrivateKey)
return privateKey, nil
}
func RSADecrypt(privateKey *rsa.PrivateKey, encryptedStr string) (string, error) {
cipherTextB64, err := base64.StdEncoding.DecodeString(encryptedStr)
if err != nil {
return "", err
}
decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherTextB64)
if err != nil {
return "", err
}
return string(decryptedBytes), nil
}
func RSAEncrypt(publicKey *rsa.PublicKey, str string) (string, error) {
encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(str))
if err != nil {
return "", err
}
encryptedStr := base64.StdEncoding.EncodeToString(encryptedBytes)
return encryptedStr, nil
}
func GetPrivateKey() *rsa.PrivateKey {
return GetConfig().App.PrivateKey
}
func GetPublicKey() *rsa.PublicKey {
return GetConfig().App.PublicKey
}
func GetPublicKeyStr() string {
return GetConfig().App.PublicKeyStr
}
// password加密
func GetPwdEncodingOpts() *password.Options {
return &password.Options{
SaltLen: 16,
Iterations: 64,
KeyLen: 16,
HashFunction: md5.New,
}
}
func GetEncodedPwd(pwd string) (string, string) {
salt, encodedPwd := password.Encode(pwd, GetPwdEncodingOpts())
return salt, encodedPwd
}
func VerifyEncodedPwd(pwdHeldRaw string, salt string, pwdTarget string) bool {
return password.Verify(pwdHeldRaw, salt, pwdTarget, GetPwdEncodingOpts())
}