-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
74 lines (63 loc) · 2.2 KB
/
encoding.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
package encrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"fmt"
)
// AesEncrypt 使用 AES 加密算法加密消息
// - message: 明文消息
// - secret: 密钥,长度必须是 16、24 或 32 位
// - encrypted: 加密后的消息
// - err: 错误信息
//
// 没有出错重试机制,出错直接返回 err
func AesEncrypt(message string, secret string) (encrypted string, err error) {
if block, buildAesBlockErr := aes.NewCipher([]byte(secret)); buildAesBlockErr != nil {
return "", buildAesBlockErr
} else {
plainTextBytes := []byte(message)
cipherText := make([]byte, aes.BlockSize+len(plainTextBytes))
iv := cipherText[:aes.BlockSize]
if _, buildIvErr := rand.Read(iv); buildIvErr != nil {
return "", buildIvErr
}
cipher.NewCFBEncrypter(block, iv).XORKeyStream(cipherText[aes.BlockSize:], plainTextBytes)
encryptedString := base64.StdEncoding.EncodeToString(cipherText)
return encryptedString, nil
}
}
// AesDecrypt 使用 AES 加密算法解密消息
// - encrypted: 加密后的消息
// - secret: 密钥,长度必须是 16、24 或 32 位
// - decrypted: 解密后的消息
// - err: 错误信息
//
// 没有出错重试机制,出错直接返回 err
func AesDecrypt(encrypted string, secret string) (decrypted string, err error) {
if block, buildAesBlockErr := aes.NewCipher([]byte(secret)); buildAesBlockErr != nil {
return "", buildAesBlockErr
} else {
encryptedBytes, decodeErr := base64.StdEncoding.DecodeString(encrypted)
if decodeErr != nil {
return "", decodeErr
}
if len(encryptedBytes) < aes.BlockSize {
return "", fmt.Errorf("encrypted message too short")
}
iv := encryptedBytes[:aes.BlockSize]
encryptedBytes = encryptedBytes[aes.BlockSize:]
cipher.NewCFBDecrypter(block, iv).XORKeyStream(encryptedBytes, encryptedBytes)
return fmt.Sprintf("%s", encryptedBytes), nil
}
}
// HashMD5 使用 MD5 算法计算消息的哈希值
func HashMD5(message string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(message)))
}
// HashEntryMD5 使用 MD5 算法计算结构体的哈希值
func HashEntryMD5[T any](entry T) string {
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%#v", entry))))
}