书籍列表接口新增参数
This commit is contained in:
80
utility/encrypt/aes.go
Normal file
80
utility/encrypt/aes.go
Normal file
@ -0,0 +1,80 @@
|
||||
package encrypt
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"server/internal/model"
|
||||
)
|
||||
|
||||
// 内部常量,外部无法访问
|
||||
const (
|
||||
aesKey = "K8mN2pQ9rS5vX1zA" // 16字节密钥
|
||||
aesIV = "H7jL4oP8qR6uW0yZ" // 16字节初始化向量
|
||||
)
|
||||
|
||||
// DecryptAdsData 解密广告数据并反序列化
|
||||
func DecryptAdsData(encryptedData string) (*model.AdsData, error) {
|
||||
// 解码base64数据
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(encryptedData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("base64 decode failed: %v", err)
|
||||
}
|
||||
|
||||
// 创建AES cipher
|
||||
block, err := aes.NewCipher([]byte(aesKey))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("aes new cipher failed: %v", err)
|
||||
}
|
||||
|
||||
// 检查数据长度
|
||||
if len(ciphertext) < aes.BlockSize {
|
||||
return nil, fmt.Errorf("ciphertext too short")
|
||||
}
|
||||
|
||||
// 创建CBC模式
|
||||
mode := cipher.NewCBCDecrypter(block, []byte(aesIV))
|
||||
|
||||
// 解密
|
||||
plaintext := make([]byte, len(ciphertext))
|
||||
mode.CryptBlocks(plaintext, ciphertext)
|
||||
|
||||
// 去除PKCS7填充
|
||||
plaintext, err = pkcs7Unpad(plaintext)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pkcs7 unpad failed: %v", err)
|
||||
}
|
||||
|
||||
// 反序列化JSON
|
||||
var adsData model.AdsData
|
||||
err = json.Unmarshal(plaintext, &adsData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("json unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
return &adsData, nil
|
||||
}
|
||||
|
||||
// pkcs7Unpad 去除PKCS7填充
|
||||
func pkcs7Unpad(data []byte) ([]byte, error) {
|
||||
length := len(data)
|
||||
if length == 0 {
|
||||
return nil, fmt.Errorf("invalid padding")
|
||||
}
|
||||
|
||||
padding := int(data[length-1])
|
||||
if padding > length {
|
||||
return nil, fmt.Errorf("invalid padding")
|
||||
}
|
||||
|
||||
// 验证填充
|
||||
for i := length - padding; i < length; i++ {
|
||||
if data[i] != byte(padding) {
|
||||
return nil, fmt.Errorf("invalid padding")
|
||||
}
|
||||
}
|
||||
|
||||
return data[:length-padding], nil
|
||||
}
|
||||
52
utility/encrypt/password_test.go
Normal file
52
utility/encrypt/password_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package encrypt
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncryptPassword(t *testing.T) {
|
||||
type args struct {
|
||||
password string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{"case1", args{"Yh243480917"}, "$2a$10$Q6yGw6W./eG0M9uKO1KFUeTs9FEndsPzHL0iTgvf4y/cJ9L3Rnqb.", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := EncryptPassword(tt.args.password)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("EncryptPassword() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("EncryptPassword() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComparePassword(t *testing.T) {
|
||||
type args struct {
|
||||
hashedPassword string
|
||||
password string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{"case1", args{"$2a$10$lKkwK05oskB.YPnXcGH2pupQxoK.02GDdGxWpstxc1keiWVFekhJ6", "Y"}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ComparePassword(tt.args.hashedPassword, tt.args.password); got != tt.want {
|
||||
t.Errorf("ComparePassword() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user