first commit
This commit is contained in:
235
pkg/lzkit/crypto/README.md
Normal file
235
pkg/lzkit/crypto/README.md
Normal file
@@ -0,0 +1,235 @@
|
||||
# AES 加密工具包
|
||||
|
||||
本包提供了多种加密方式,特别是用于处理敏感个人信息(如手机号、身份证号等)的加密和解密功能。
|
||||
|
||||
## 主要功能
|
||||
|
||||
- **AES-CBC 模式加密/解密** - 标准加密模式,适用于一般数据加密
|
||||
- **AES-ECB 模式加密/解密** - 确定性加密模式,适用于数据库字段加密和查询
|
||||
- **专门针对个人敏感信息的加密/解密方法**
|
||||
- **密钥生成和管理工具**
|
||||
|
||||
## 安全性说明
|
||||
|
||||
- **AES-CBC 模式**:使用随机 IV,相同明文每次加密结果不同,安全性较高
|
||||
- **AES-ECB 模式**:确定性加密,相同明文每次加密结果相同,便于数据库查询,但安全性较低
|
||||
|
||||
> **⚠️ 警告**:ECB 模式仅适用于短文本(如手机号、身份证号)的确定性加密,不建议用于加密大段文本或高安全需求场景。
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 1. 加密手机号
|
||||
|
||||
使用 AES-ECB 模式加密手机号,保证确定性(相同手机号总是产生相同密文):
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"hm-server/pkg/lzkit/crypto"
|
||||
)
|
||||
|
||||
func encryptMobileExample() {
|
||||
// 您的密钥(需安全保存,建议存储在配置中)
|
||||
key := []byte("1234567890abcdef") // 16字节AES-128密钥
|
||||
|
||||
// 加密手机号
|
||||
mobile := "13800138000"
|
||||
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("加密后的手机号:", encryptedMobile)
|
||||
|
||||
// 解密手机号
|
||||
decryptedMobile, err := crypto.DecryptMobile(encryptedMobile, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("解密后的手机号:", decryptedMobile)
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 在数据库中存储和查询加密手机号
|
||||
|
||||
```go
|
||||
// 加密并存储手机号
|
||||
func saveUser(db *sqlx.DB, mobile string, key []byte) (int64, error) {
|
||||
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var id int64
|
||||
err = db.QueryRow(
|
||||
"INSERT INTO users (mobile, create_time) VALUES (?, NOW()) RETURNING id",
|
||||
encryptedMobile,
|
||||
).Scan(&id)
|
||||
|
||||
return id, err
|
||||
}
|
||||
|
||||
// 根据手机号查询用户
|
||||
func findUserByMobile(db *sqlx.DB, mobile string, key []byte) (*User, error) {
|
||||
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var user User
|
||||
err = db.QueryRow(
|
||||
"SELECT id, mobile, create_time FROM users WHERE mobile = ?",
|
||||
encryptedMobile,
|
||||
).Scan(&user.ID, &user.EncryptedMobile, &user.CreateTime)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 解密手机号用于显示
|
||||
user.Mobile, _ = crypto.DecryptMobile(user.EncryptedMobile, key)
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 加密身份证号
|
||||
|
||||
```go
|
||||
func encryptIDCardExample() {
|
||||
key := []byte("1234567890abcdef")
|
||||
|
||||
idCard := "440101199001011234"
|
||||
encryptedIDCard, err := crypto.EncryptIDCard(idCard, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("加密后的身份证号:", encryptedIDCard)
|
||||
|
||||
// 解密身份证号
|
||||
decryptedIDCard, err := crypto.DecryptIDCard(encryptedIDCard, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("解密后的身份证号:", decryptedIDCard)
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 密钥管理
|
||||
|
||||
```go
|
||||
func keyManagementExample() {
|
||||
// 生成随机密钥
|
||||
key, err := crypto.GenerateAESKey(16) // AES-128
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("生成的密钥(十六进制): %x\n", key)
|
||||
|
||||
// 从密码派生密钥(便于记忆)
|
||||
password := "my-secure-password"
|
||||
derivedKey, err := crypto.DeriveKeyFromPassword(password, 16)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("从密码派生的密钥: %x\n", derivedKey)
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 使用十六进制输出(适用于 URL 参数)
|
||||
|
||||
```go
|
||||
func hexEncodingExample() {
|
||||
key := []byte("1234567890abcdef")
|
||||
mobile := "13800138000"
|
||||
|
||||
// 使用十六进制编码(适合URL参数)
|
||||
encryptedHex, err := crypto.EncryptMobileHex(mobile, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("十六进制编码的加密手机号:", encryptedHex)
|
||||
|
||||
// 解密十六进制编码的手机号
|
||||
decryptedMobile, err := crypto.DecryptMobileHex(encryptedHex, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("解密后的手机号:", decryptedMobile)
|
||||
}
|
||||
```
|
||||
|
||||
## 在 Go-Zero 项目中使用
|
||||
|
||||
在 Go-Zero 项目中,建议将加密密钥放在配置文件中:
|
||||
|
||||
1. 在配置文件中添加密钥配置:
|
||||
|
||||
```yaml
|
||||
# etc/main.yaml
|
||||
Name: main-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
|
||||
Encrypt:
|
||||
MobileKey: "1234567890abcdef" # 16字节AES-128密钥
|
||||
IDCardKey: "1234567890abcdef1234567890abcdef" # 32字节AES-256密钥
|
||||
```
|
||||
|
||||
2. 在配置结构中定义:
|
||||
|
||||
```go
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Encrypt struct {
|
||||
MobileKey string
|
||||
IDCardKey string
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. 在服务上下文中使用:
|
||||
|
||||
```go
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
UserModel model.UserModel
|
||||
MobileKey []byte
|
||||
IDCardKey []byte
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
UserModel: model.NewUserModel(sqlx.NewMysql(c.DB.DataSource), c.Cache),
|
||||
MobileKey: []byte(c.Encrypt.MobileKey),
|
||||
IDCardKey: []byte(c.Encrypt.IDCardKey),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. 在 Logic 中使用:
|
||||
|
||||
```go
|
||||
func (l *RegisterLogic) Register(req *types.RegisterReq) (*types.RegisterResp, error) {
|
||||
// 加密手机号用于存储
|
||||
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, l.svcCtx.MobileKey)
|
||||
if err != nil {
|
||||
return nil, errors.New("手机号加密失败")
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
user := &model.User{
|
||||
Mobile: encryptedMobile,
|
||||
// 其他字段...
|
||||
}
|
||||
|
||||
result, err := l.svcCtx.UserModel.Insert(l.ctx, nil, user)
|
||||
// 其余逻辑...
|
||||
}
|
||||
```
|
||||
100
pkg/lzkit/crypto/README_bcrypt_test.md
Normal file
100
pkg/lzkit/crypto/README_bcrypt_test.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# bcrypt 密码加密测试文档
|
||||
|
||||
## 功能概述
|
||||
|
||||
为 `pkg/lzkit/crypto/bcrypt.go` 中的 `PasswordHash` 和 `PasswordVerify` 函数创建了完整的测试套件。
|
||||
|
||||
## 测试文件
|
||||
|
||||
- **文件位置**: `pkg/lzkit/crypto/bcrypt_test.go`
|
||||
- **测试函数**:
|
||||
- `TestPasswordHash` - 测试密码加密功能
|
||||
- `TestPasswordVerify` - 测试密码验证功能
|
||||
- `TestGeneratePasswords` - 生成常用密码的hash值
|
||||
- `BenchmarkPasswordHash` - 性能测试
|
||||
- `BenchmarkPasswordVerify` - 验证性能测试
|
||||
|
||||
## 测试覆盖
|
||||
|
||||
### 1. 密码加密测试 (`TestPasswordHash`)
|
||||
- ✅ 默认cost加密 (cost=10)
|
||||
- ✅ 自定义cost加密 (cost=12)
|
||||
- ✅ 空密码处理
|
||||
- ✅ 复杂密码处理
|
||||
- ✅ 密码验证功能
|
||||
- ✅ 错误密码验证
|
||||
|
||||
### 2. 密码验证测试 (`TestPasswordVerify`)
|
||||
- ✅ 正确密码验证
|
||||
- ✅ 错误密码验证
|
||||
|
||||
### 3. 密码生成测试 (`TestGeneratePasswords`)
|
||||
生成10个常用密码的hash值,包括:
|
||||
- `123456`
|
||||
- `admin123`
|
||||
- `password`
|
||||
- `root`
|
||||
- `test123`
|
||||
- `MyP@ssw0rd!2024`
|
||||
- `admin@123`
|
||||
- `123456789`
|
||||
- `qwerty`
|
||||
- `abc123`
|
||||
|
||||
## 性能测试结果
|
||||
|
||||
### Cost=10 性能
|
||||
- **执行时间**: ~41.7ms/op
|
||||
- **内存分配**: 5314 B/op, 11 allocs/op
|
||||
|
||||
### Cost=12 性能
|
||||
- **执行时间**: ~164.1ms/op
|
||||
- **内存分配**: 5691 B/op, 12 allocs/op
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 运行所有测试
|
||||
```bash
|
||||
cd pkg/lzkit/crypto
|
||||
go test -v
|
||||
```
|
||||
|
||||
### 运行密码相关测试
|
||||
```bash
|
||||
go test -run "TestPassword|TestGenerate" -v
|
||||
```
|
||||
|
||||
### 运行性能测试
|
||||
```bash
|
||||
go test -bench=BenchmarkPassword -benchmem -run="^$"
|
||||
```
|
||||
|
||||
### 生成密码hash
|
||||
```bash
|
||||
go test -run TestGeneratePasswords -v
|
||||
```
|
||||
|
||||
## 示例输出
|
||||
|
||||
```
|
||||
=== 生成密码Hash值 ===
|
||||
1. 密码: 123456 Hash: $2a$10$AXcpNL9y5RYLiObTLFq4KOWKtlV3jEUuCd6fuzmSW2yYsSELJ23D.
|
||||
2. 密码: admin123 Hash: $2a$10$5PUD/kpFGJ.09Gi.VGzu2.sCp9ZEshEcCaP4tKPNMgbvOaY8Hq7Sy
|
||||
3. 密码: password Hash: $2a$10$Tjl5JY13eyGE4tPUdbco0OToz2iN6UY3Dm/QTYUpZx3b5QAPH4Aq6
|
||||
...
|
||||
=== 密码生成完成 ===
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **Cost参数**: 默认使用cost=10,可根据安全需求调整
|
||||
2. **性能考虑**: Cost越高越安全,但性能消耗越大
|
||||
3. **Hash唯一性**: 每次生成的hash都不同,但验证结果一致
|
||||
4. **安全性**: 使用bcrypt算法,适合生产环境使用
|
||||
|
||||
## 测试状态
|
||||
|
||||
✅ 所有bcrypt相关测试通过
|
||||
✅ 性能测试完成
|
||||
✅ 密码生成功能正常
|
||||
✅ 验证功能正常
|
||||
28
pkg/lzkit/crypto/bcrypt.go
Normal file
28
pkg/lzkit/crypto/bcrypt.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// PasswordHash 使用bcrypt对密码进行加密
|
||||
// cost参数确定加密的复杂度,默认为10,越高越安全但性能消耗越大
|
||||
func PasswordHash(password string, cost ...int) (string, error) {
|
||||
defaultCost := 10
|
||||
if len(cost) > 0 && cost[0] > 0 {
|
||||
defaultCost = cost[0]
|
||||
}
|
||||
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), defaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
// PasswordVerify 验证密码是否匹配
|
||||
// password是用户输入的明文密码,hash是存储的加密密码
|
||||
func PasswordVerify(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
193
pkg/lzkit/crypto/bcrypt_test.go
Normal file
193
pkg/lzkit/crypto/bcrypt_test.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPasswordHash 测试密码加密功能
|
||||
func TestPasswordHash(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
password string
|
||||
cost int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "默认cost加密",
|
||||
password: "123456",
|
||||
cost: 0, // 使用默认cost
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "自定义cost加密",
|
||||
password: "admin123",
|
||||
cost: 12,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "空密码",
|
||||
password: "",
|
||||
cost: 10,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "复杂密码",
|
||||
password: "MyP@ssw0rd!2024",
|
||||
cost: 11,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var hash string
|
||||
var err error
|
||||
|
||||
if tc.cost > 0 {
|
||||
hash, err = PasswordHash(tc.password, tc.cost)
|
||||
} else {
|
||||
hash, err = PasswordHash(tc.password)
|
||||
}
|
||||
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("PasswordHash() 期望出错,但没有错误")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("PasswordHash() 出现错误 = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if hash == "" {
|
||||
t.Errorf("PasswordHash() 返回空字符串")
|
||||
return
|
||||
}
|
||||
|
||||
// 验证生成的hash长度合理(bcrypt hash通常是60字符)
|
||||
if len(hash) < 50 {
|
||||
t.Errorf("PasswordHash() 返回的hash太短 = %d", len(hash))
|
||||
}
|
||||
|
||||
// 验证密码验证功能
|
||||
if !PasswordVerify(tc.password, hash) {
|
||||
t.Errorf("PasswordVerify() 验证失败,密码不匹配")
|
||||
}
|
||||
|
||||
// 验证错误密码
|
||||
if PasswordVerify("wrongpassword", hash) {
|
||||
t.Errorf("PasswordVerify() 应该验证失败,但验证成功了")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPasswordVerify 测试密码验证功能
|
||||
func TestPasswordVerify(t *testing.T) {
|
||||
// 先生成一个已知的hash用于测试
|
||||
testPassword := "123456"
|
||||
testHash, err := PasswordHash(testPassword, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("生成测试hash失败: %v", err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
password string
|
||||
hash string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "正确密码",
|
||||
password: testPassword,
|
||||
hash: testHash,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "错误密码",
|
||||
password: "wrongpassword",
|
||||
hash: testHash,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := PasswordVerify(tc.password, tc.hash)
|
||||
if got != tc.want {
|
||||
t.Errorf("PasswordVerify() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeneratePasswords 生成常用密码的hash值(用于实际使用)
|
||||
func TestGeneratePasswords(t *testing.T) {
|
||||
// 常用密码列表
|
||||
passwords := []string{
|
||||
"123456",
|
||||
"admin123",
|
||||
"password",
|
||||
"root",
|
||||
"test123",
|
||||
"MyP@ssw0rd!2024",
|
||||
"admin123.",
|
||||
"123456789",
|
||||
"qwerty",
|
||||
"abc123",
|
||||
}
|
||||
|
||||
fmt.Println("\n=== 生成密码Hash值 ===")
|
||||
for i, password := range passwords {
|
||||
hash, err := PasswordHash(password)
|
||||
if err != nil {
|
||||
t.Errorf("生成密码 %s 的hash失败: %v", password, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("%d. 密码: %-20s Hash: %s\n", i+1, password, hash)
|
||||
|
||||
// 验证生成的hash是否正确
|
||||
if !PasswordVerify(password, hash) {
|
||||
t.Errorf("验证密码 %s 失败", password)
|
||||
}
|
||||
}
|
||||
fmt.Println("=== 密码生成完成 ===")
|
||||
}
|
||||
|
||||
// BenchmarkPasswordHash 性能测试
|
||||
func BenchmarkPasswordHash(b *testing.B) {
|
||||
password := "testpassword123"
|
||||
|
||||
b.Run("Cost10", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := PasswordHash(password, 10)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("Cost12", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := PasswordHash(password, 12)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// BenchmarkPasswordVerify 验证性能测试
|
||||
func BenchmarkPasswordVerify(b *testing.B) {
|
||||
password := "testpassword123"
|
||||
hash, _ := PasswordHash(password, 10)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
PasswordVerify(password, hash)
|
||||
}
|
||||
}
|
||||
105
pkg/lzkit/crypto/crypto.go
Normal file
105
pkg/lzkit/crypto/crypto.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// PKCS7填充
|
||||
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
// 去除PKCS7填充
|
||||
func PKCS7UnPadding(origData []byte) ([]byte, error) {
|
||||
length := len(origData)
|
||||
if length == 0 {
|
||||
return nil, errors.New("input data error")
|
||||
}
|
||||
unpadding := int(origData[length-1])
|
||||
if unpadding > length {
|
||||
return nil, errors.New("unpadding size is invalid")
|
||||
}
|
||||
|
||||
// 检查填充字节是否一致
|
||||
for i := 0; i < unpadding; i++ {
|
||||
if origData[length-1-i] != byte(unpadding) {
|
||||
return nil, errors.New("invalid padding")
|
||||
}
|
||||
}
|
||||
|
||||
return origData[:(length - unpadding)], nil
|
||||
}
|
||||
|
||||
// AES CBC模式加密,Base64传入传出
|
||||
func AesEncrypt(plainText, key []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
plainText = PKCS7Padding(plainText, blockSize)
|
||||
|
||||
cipherText := make([]byte, blockSize+len(plainText))
|
||||
iv := cipherText[:blockSize] // 使用前blockSize字节作为IV
|
||||
_, err = io.ReadFull(rand.Reader, iv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText[blockSize:], plainText)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
// AES CBC模式解密,Base64传入传出
|
||||
func AesDecrypt(cipherTextBase64 string, key []byte) ([]byte, error) {
|
||||
cipherText, err := base64.StdEncoding.DecodeString(cipherTextBase64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockSize := block.BlockSize()
|
||||
if len(cipherText) < blockSize {
|
||||
return nil, errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
iv := cipherText[:blockSize]
|
||||
cipherText = cipherText[blockSize:]
|
||||
|
||||
if len(cipherText)%blockSize != 0 {
|
||||
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, cipherText)
|
||||
|
||||
plainText, err := PKCS7UnPadding(cipherText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// Md5Encrypt 用于对传入的message进行MD5加密
|
||||
func Md5Encrypt(message string) string {
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(message)) // 将字符串转换为字节切片并写入
|
||||
return hex.EncodeToString(hash.Sum(nil)) // 将哈希值转换为16进制字符串并返回
|
||||
}
|
||||
67
pkg/lzkit/crypto/crypto_url.go
Normal file
67
pkg/lzkit/crypto/crypto_url.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// AES CBC模式加密,Base64传入传出
|
||||
func AesEncryptURL(plainText, key []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
plainText = PKCS7Padding(plainText, blockSize)
|
||||
|
||||
cipherText := make([]byte, blockSize+len(plainText))
|
||||
iv := cipherText[:blockSize] // 使用前blockSize字节作为IV
|
||||
_, err = io.ReadFull(rand.Reader, iv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText[blockSize:], plainText)
|
||||
|
||||
return base64.URLEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
// AES CBC模式解密,Base64传入传出
|
||||
func AesDecryptURL(cipherTextBase64 string, key []byte) ([]byte, error) {
|
||||
cipherText, err := base64.URLEncoding.DecodeString(cipherTextBase64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockSize := block.BlockSize()
|
||||
if len(cipherText) < blockSize {
|
||||
return nil, errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
iv := cipherText[:blockSize]
|
||||
cipherText = cipherText[blockSize:]
|
||||
|
||||
if len(cipherText)%blockSize != 0 {
|
||||
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, cipherText)
|
||||
|
||||
plainText, err := PKCS7UnPadding(cipherText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
||||
274
pkg/lzkit/crypto/ecb.go
Normal file
274
pkg/lzkit/crypto/ecb.go
Normal file
@@ -0,0 +1,274 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ECB模式是一种基本的加密模式,每个明文块独立加密
|
||||
// 警告:ECB模式存在安全问题,仅用于需要确定性加密的场景,如数据库字段查询
|
||||
// 不要用于加密大段文本或安全要求高的场景
|
||||
|
||||
// 验证密钥长度是否有效 (AES-128, AES-192, AES-256)
|
||||
func validateAESKey(key []byte) error {
|
||||
switch len(key) {
|
||||
case 16, 24, 32:
|
||||
return nil
|
||||
default:
|
||||
return errors.New("AES密钥长度必须是16、24或32字节(对应AES-128、AES-192、AES-256)")
|
||||
}
|
||||
}
|
||||
|
||||
// AesEcbEncrypt AES-ECB模式加密,返回Base64编码的密文
|
||||
// 使用已有的ECB实现,但提供更易用的接口
|
||||
func AesEcbEncrypt(plainText, key []byte) (string, error) {
|
||||
if err := validateAESKey(key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 使用PKCS7填充
|
||||
plainText = PKCS7Padding(plainText, block.BlockSize())
|
||||
|
||||
// 创建密文数组
|
||||
cipherText := make([]byte, len(plainText))
|
||||
|
||||
// ECB模式加密,使用west_crypto.go中已有的实现
|
||||
mode := newECBEncrypter(block)
|
||||
mode.CryptBlocks(cipherText, plainText)
|
||||
|
||||
// 返回Base64编码的密文
|
||||
return base64.StdEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
// AesEcbDecrypt AES-ECB模式解密,输入Base64编码的密文
|
||||
func AesEcbDecrypt(cipherTextBase64 string, key []byte) ([]byte, error) {
|
||||
if err := validateAESKey(key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Base64解码
|
||||
cipherText, err := base64.StdEncoding.DecodeString(cipherTextBase64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查密文长度
|
||||
if len(cipherText)%block.BlockSize() != 0 {
|
||||
return nil, errors.New("密文长度必须是块大小的整数倍")
|
||||
}
|
||||
|
||||
// 创建明文数组
|
||||
plainText := make([]byte, len(cipherText))
|
||||
|
||||
// ECB模式解密,使用west_crypto.go中已有的实现
|
||||
mode := newECBDecrypter(block)
|
||||
mode.CryptBlocks(plainText, cipherText)
|
||||
|
||||
// 去除PKCS7填充
|
||||
plainText, err = PKCS7UnPadding(plainText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// AesEcbEncryptHex AES-ECB模式加密,返回十六进制编码的密文
|
||||
func AesEcbEncryptHex(plainText, key []byte) (string, error) {
|
||||
if err := validateAESKey(key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 使用PKCS7填充
|
||||
plainText = PKCS7Padding(plainText, block.BlockSize())
|
||||
|
||||
// 创建密文数组
|
||||
cipherText := make([]byte, len(plainText))
|
||||
|
||||
// ECB模式加密
|
||||
mode := newECBEncrypter(block)
|
||||
mode.CryptBlocks(cipherText, plainText)
|
||||
|
||||
// 返回十六进制编码的密文
|
||||
return hex.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
// AesEcbDecryptHex AES-ECB模式解密,输入十六进制编码的密文
|
||||
func AesEcbDecryptHex(cipherTextHex string, key []byte) ([]byte, error) {
|
||||
if err := validateAESKey(key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 十六进制解码
|
||||
cipherText, err := hex.DecodeString(cipherTextHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查密文长度
|
||||
if len(cipherText)%block.BlockSize() != 0 {
|
||||
return nil, errors.New("密文长度必须是块大小的整数倍")
|
||||
}
|
||||
|
||||
// 创建明文数组
|
||||
plainText := make([]byte, len(cipherText))
|
||||
|
||||
// ECB模式解密
|
||||
mode := newECBDecrypter(block)
|
||||
mode.CryptBlocks(plainText, cipherText)
|
||||
|
||||
// 去除PKCS7填充
|
||||
plainText, err = PKCS7UnPadding(plainText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// 以下是专门用于处理手机号等敏感数据的实用函数
|
||||
|
||||
// EncryptMobile 使用AES-ECB加密手机号,返回Base64编码
|
||||
// 该方法保证对相同手机号总是产生相同密文,便于数据库查询
|
||||
func EncryptMobile(mobile string, secretKey string) (string, error) {
|
||||
key, decodeErr := hex.DecodeString(secretKey)
|
||||
if decodeErr != nil {
|
||||
return "", decodeErr
|
||||
}
|
||||
if mobile == "" {
|
||||
return "", errors.New("手机号不能为空")
|
||||
}
|
||||
return AesEcbEncrypt([]byte(mobile), key)
|
||||
}
|
||||
|
||||
// DecryptMobile 解密手机号
|
||||
func DecryptMobile(encryptedMobile string, secretKey string) (string, error) {
|
||||
key, decodeErr := hex.DecodeString(secretKey)
|
||||
if decodeErr != nil {
|
||||
return "", decodeErr
|
||||
}
|
||||
if encryptedMobile == "" {
|
||||
return "", errors.New("加密手机号不能为空")
|
||||
}
|
||||
|
||||
bytes, err := AesEcbDecrypt(encryptedMobile, key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解密手机号失败: %v", err)
|
||||
}
|
||||
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
// EncryptMobileHex 使用AES-ECB加密手机号,返回十六进制编码(适用于URL参数)
|
||||
func EncryptMobileHex(mobile string, key []byte) (string, error) {
|
||||
if mobile == "" {
|
||||
return "", errors.New("手机号不能为空")
|
||||
}
|
||||
return AesEcbEncryptHex([]byte(mobile), key)
|
||||
}
|
||||
|
||||
// DecryptMobileHex 解密十六进制编码的手机号
|
||||
func DecryptMobileHex(encryptedMobileHex string, key []byte) (string, error) {
|
||||
if encryptedMobileHex == "" {
|
||||
return "", errors.New("加密手机号不能为空")
|
||||
}
|
||||
|
||||
bytes, err := AesEcbDecryptHex(encryptedMobileHex, key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解密手机号失败: %v", err)
|
||||
}
|
||||
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
// EncryptIDCard 使用AES-ECB加密身份证号
|
||||
func EncryptIDCard(idCard string, key []byte) (string, error) {
|
||||
if idCard == "" {
|
||||
return "", errors.New("身份证号不能为空")
|
||||
}
|
||||
return AesEcbEncrypt([]byte(idCard), key)
|
||||
}
|
||||
|
||||
// DecryptIDCard 解密身份证号
|
||||
func DecryptIDCard(encryptedIDCard string, key []byte) (string, error) {
|
||||
if encryptedIDCard == "" {
|
||||
return "", errors.New("加密身份证号不能为空")
|
||||
}
|
||||
|
||||
bytes, err := AesEcbDecrypt(encryptedIDCard, key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解密身份证号失败: %v", err)
|
||||
}
|
||||
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
// IsEncrypted 检查字符串是否为Base64编码的加密数据
|
||||
func IsEncrypted(data string) bool {
|
||||
// 检查是否是有效的Base64编码
|
||||
_, err := base64.StdEncoding.DecodeString(data)
|
||||
return err == nil && len(data) >= 20 // 至少20个字符的Base64字符串
|
||||
}
|
||||
|
||||
// GenerateAESKey 生成AES密钥
|
||||
// keySize: 可选16, 24, 32字节(对应AES-128, AES-192, AES-256)
|
||||
func GenerateAESKey(keySize int) ([]byte, error) {
|
||||
if keySize != 16 && keySize != 24 && keySize != 32 {
|
||||
return nil, errors.New("密钥长度必须是16、24或32字节")
|
||||
}
|
||||
|
||||
key := make([]byte, keySize)
|
||||
_, err := rand.Read(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// DeriveKeyFromPassword 基于密码派生固定长度的AES密钥
|
||||
func DeriveKeyFromPassword(password string, keySize int) ([]byte, error) {
|
||||
if keySize != 16 && keySize != 24 && keySize != 32 {
|
||||
return nil, errors.New("密钥长度必须是16、24或32字节")
|
||||
}
|
||||
|
||||
// 使用PBKDF2或简单的方法从密码派生密钥
|
||||
// 这里使用简单的MD5方法,实际生产环境应使用更安全的PBKDF2
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(password))
|
||||
key := hash.Sum(nil) // 16字节
|
||||
|
||||
// 如果需要24或32字节,继续哈希
|
||||
if keySize > 16 {
|
||||
hash.Reset()
|
||||
hash.Write(key)
|
||||
key = append(key, hash.Sum(nil)[:keySize-16]...)
|
||||
}
|
||||
|
||||
return key, nil
|
||||
}
|
||||
186
pkg/lzkit/crypto/ecb_test.go
Normal file
186
pkg/lzkit/crypto/ecb_test.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAesEcbMobileEncryption(t *testing.T) {
|
||||
// 测试手机号加密
|
||||
mobile := "18653052547"
|
||||
key := []byte("ff83609b2b24fc73196aac3d3dfb874f") // 16字节AES-128密钥
|
||||
|
||||
keyStr := hex.EncodeToString(key)
|
||||
// 测试加密
|
||||
encrypted, err := EncryptMobile(mobile, keyStr)
|
||||
if err != nil {
|
||||
t.Fatalf("手机号加密失败: %v", err)
|
||||
}
|
||||
fmt.Printf("encrypted: %s\n", encrypted)
|
||||
jmStr := "m9EEeW9ZBBJmi1hx1k1uIQ=="
|
||||
// 测试解密
|
||||
decrypted, err := DecryptMobile(jmStr, keyStr)
|
||||
if err != nil {
|
||||
t.Fatalf("手机号解密失败: %v", err)
|
||||
}
|
||||
fmt.Printf("decrypted: %s\n", decrypted)
|
||||
// 验证结果
|
||||
if decrypted != mobile {
|
||||
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", mobile, decrypted)
|
||||
}
|
||||
|
||||
// 测试相同输入产生相同输出(确定性)
|
||||
encrypted2, _ := EncryptMobile(mobile, keyStr)
|
||||
if encrypted != encrypted2 {
|
||||
t.Errorf("AES-ECB不是确定性的,两次加密结果不同: %s vs %s", encrypted, encrypted2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAesEcbHexEncryption(t *testing.T) {
|
||||
// 测试十六进制编码加密
|
||||
idCard := "440101199001011234"
|
||||
key := []byte("1234567890abcdef") // 16字节AES-128密钥
|
||||
|
||||
// 测试HEX加密
|
||||
encryptedHex, err := EncryptIDCard(idCard, key)
|
||||
if err != nil {
|
||||
t.Fatalf("身份证加密失败: %v", err)
|
||||
}
|
||||
|
||||
// 测试HEX解密
|
||||
decrypted, err := DecryptIDCard(encryptedHex, key)
|
||||
if err != nil {
|
||||
t.Fatalf("身份证解密失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证结果
|
||||
if decrypted != idCard {
|
||||
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", idCard, decrypted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAesEcbKeyValidation(t *testing.T) {
|
||||
// 测试不同长度的密钥
|
||||
validKeys := [][]byte{
|
||||
make([]byte, 16), // AES-128
|
||||
make([]byte, 24), // AES-192
|
||||
make([]byte, 32), // AES-256
|
||||
}
|
||||
|
||||
invalidKeys := [][]byte{
|
||||
make([]byte, 15),
|
||||
make([]byte, 20),
|
||||
make([]byte, 33),
|
||||
}
|
||||
|
||||
text := []byte("test text")
|
||||
|
||||
// 测试有效密钥
|
||||
for _, key := range validKeys {
|
||||
_, err := AesEcbEncrypt(text, key)
|
||||
if err != nil {
|
||||
t.Errorf("有效密钥(%d字节)校验失败: %v", len(key), err)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试无效密钥
|
||||
for _, key := range invalidKeys {
|
||||
_, err := AesEcbEncrypt(text, key)
|
||||
if err == nil {
|
||||
t.Errorf("无效密钥(%d字节)未被检测出", len(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEncrypted(t *testing.T) {
|
||||
// 有效的Base64编码字符串
|
||||
validBase64 := base64.StdEncoding.EncodeToString([]byte("这是一个足够长的字符串,以通过IsEncrypted检查"))
|
||||
|
||||
// 无效的字符串
|
||||
invalidStrings := []string{
|
||||
"",
|
||||
"abc",
|
||||
"not-base64!@#",
|
||||
hex.EncodeToString([]byte("hexstring")),
|
||||
}
|
||||
|
||||
// 测试有效的加密数据
|
||||
if !IsEncrypted(validBase64) {
|
||||
t.Errorf("有效的Base64未被识别为加密数据: %s", validBase64)
|
||||
}
|
||||
|
||||
// 测试无效的数据
|
||||
for _, s := range invalidStrings {
|
||||
if IsEncrypted(s) {
|
||||
t.Errorf("无效字符串被错误识别为加密数据: %s", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveKeyFromPassword(t *testing.T) {
|
||||
password := "my-secure-password"
|
||||
|
||||
// 测试不同长度的派生密钥
|
||||
keySizes := []int{16, 24, 32}
|
||||
|
||||
for _, size := range keySizes {
|
||||
key, err := DeriveKeyFromPassword(password, size)
|
||||
if err != nil {
|
||||
t.Errorf("从密码派生%d字节密钥失败: %v", size, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(key) != size {
|
||||
t.Errorf("派生的密钥长度错误,期望: %d, 实际: %d", size, len(key))
|
||||
}
|
||||
|
||||
// 测试相同密码总是产生相同密钥
|
||||
key2, _ := DeriveKeyFromPassword(password, size)
|
||||
if string(key) != string(key2) {
|
||||
t.Errorf("从相同密码派生的密钥不一致")
|
||||
}
|
||||
|
||||
// 使用派生的密钥加密测试
|
||||
_, err = AesEcbEncrypt([]byte("test"), key)
|
||||
if err != nil {
|
||||
t.Errorf("使用派生的密钥加密失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试无效的密钥大小
|
||||
_, err := DeriveKeyFromPassword(password, 18)
|
||||
if err == nil {
|
||||
t.Error("无效的密钥大小未被检测出")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateAESKey(t *testing.T) {
|
||||
// 测试生成不同长度的密钥
|
||||
keySizes := []int{16, 24, 32}
|
||||
|
||||
for _, size := range keySizes {
|
||||
key, err := GenerateAESKey(size)
|
||||
if err != nil {
|
||||
t.Errorf("生成%d字节密钥失败: %v", size, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(key) != size {
|
||||
t.Errorf("生成的密钥长度错误,期望: %d, 实际: %d", size, len(key))
|
||||
}
|
||||
|
||||
// 使用生成的密钥加密测试
|
||||
_, err = AesEcbEncrypt([]byte("test"), key)
|
||||
if err != nil {
|
||||
t.Errorf("使用生成的密钥加密失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试无效的密钥大小
|
||||
_, err := GenerateAESKey(18)
|
||||
if err == nil {
|
||||
t.Error("无效的密钥大小未被检测出")
|
||||
}
|
||||
}
|
||||
63
pkg/lzkit/crypto/generate.go
Normal file
63
pkg/lzkit/crypto/generate.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
mathrand "math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 生成AES-128密钥的函数,符合市面规范
|
||||
func GenerateSecretKey() (string, error) {
|
||||
key := make([]byte, 16) // 16字节密钥
|
||||
_, err := io.ReadFull(rand.Reader, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(key), nil
|
||||
}
|
||||
|
||||
func GenerateSecretId() (string, error) {
|
||||
// 创建一个字节数组,用于存储随机数据
|
||||
bytes := make([]byte, 8) // 因为每个字节表示两个16进制字符
|
||||
|
||||
// 读取随机字节到数组中
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 将字节数组转换为16进制字符串
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
// GenerateTransactionID 生成16位数的交易单号
|
||||
func GenerateTransactionID() string {
|
||||
length := 16
|
||||
// 获取当前时间戳
|
||||
timestamp := time.Now().UnixNano()
|
||||
|
||||
// 转换为字符串
|
||||
timeStr := strconv.FormatInt(timestamp, 10)
|
||||
|
||||
// 生成随机数
|
||||
mathrand.Seed(time.Now().UnixNano())
|
||||
randomPart := strconv.Itoa(mathrand.Intn(1000000))
|
||||
|
||||
// 组合时间戳和随机数
|
||||
combined := timeStr + randomPart
|
||||
|
||||
// 如果长度超出指定值,则截断;如果不够,则填充随机字符
|
||||
if len(combined) >= length {
|
||||
return combined[:length]
|
||||
}
|
||||
|
||||
// 如果长度不够,填充0
|
||||
for len(combined) < length {
|
||||
combined += strconv.Itoa(mathrand.Intn(10)) // 填充随机数
|
||||
}
|
||||
|
||||
return combined
|
||||
}
|
||||
150
pkg/lzkit/crypto/west_crypto.go
Normal file
150
pkg/lzkit/crypto/west_crypto.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
const (
|
||||
KEY_SIZE = 16 // AES-128, 16 bytes
|
||||
)
|
||||
|
||||
// Encrypt encrypts the given data using AES encryption in ECB mode with PKCS5 padding
|
||||
func WestDexEncrypt(data, secretKey string) (string, error) {
|
||||
key := generateAESKey(KEY_SIZE*8, []byte(secretKey))
|
||||
ciphertext, err := aesEncrypt([]byte(data), key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// Decrypt decrypts the given base64-encoded string using AES encryption in ECB mode with PKCS5 padding
|
||||
func WestDexDecrypt(encodedData, secretKey string) ([]byte, error) {
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(encodedData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := generateAESKey(KEY_SIZE*8, []byte(secretKey))
|
||||
plaintext, err := aesDecrypt(ciphertext, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
// generateAESKey generates a key for AES encryption using a SHA-1 based PRNG
|
||||
func generateAESKey(length int, password []byte) []byte {
|
||||
h := sha1.New()
|
||||
h.Write(password)
|
||||
state := h.Sum(nil)
|
||||
|
||||
keyBytes := make([]byte, 0, length/8)
|
||||
for len(keyBytes) < length/8 {
|
||||
h := sha1.New()
|
||||
h.Write(state)
|
||||
state = h.Sum(nil)
|
||||
keyBytes = append(keyBytes, state...)
|
||||
}
|
||||
|
||||
return keyBytes[:length/8]
|
||||
}
|
||||
|
||||
// aesEncrypt encrypts plaintext using AES in ECB mode with PKCS5 padding
|
||||
func aesEncrypt(plaintext, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paddedPlaintext := pkcs5Padding(plaintext, block.BlockSize())
|
||||
ciphertext := make([]byte, len(paddedPlaintext))
|
||||
mode := newECBEncrypter(block)
|
||||
mode.CryptBlocks(ciphertext, paddedPlaintext)
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// aesDecrypt decrypts ciphertext using AES in ECB mode with PKCS5 padding
|
||||
func aesDecrypt(ciphertext, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plaintext := make([]byte, len(ciphertext))
|
||||
mode := newECBDecrypter(block)
|
||||
mode.CryptBlocks(plaintext, ciphertext)
|
||||
return pkcs5Unpadding(plaintext), nil
|
||||
}
|
||||
|
||||
// pkcs5Padding pads the input to a multiple of the block size using PKCS5 padding
|
||||
func pkcs5Padding(src []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(src)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(src, padtext...)
|
||||
}
|
||||
|
||||
// pkcs5Unpadding removes PKCS5 padding from the input
|
||||
func pkcs5Unpadding(src []byte) []byte {
|
||||
length := len(src)
|
||||
unpadding := int(src[length-1])
|
||||
return src[:(length - unpadding)]
|
||||
}
|
||||
|
||||
// ECB mode encryption/decryption
|
||||
type ecb struct {
|
||||
b cipher.Block
|
||||
blockSize int
|
||||
}
|
||||
|
||||
func newECB(b cipher.Block) *ecb {
|
||||
return &ecb{
|
||||
b: b,
|
||||
blockSize: b.BlockSize(),
|
||||
}
|
||||
}
|
||||
|
||||
type ecbEncrypter ecb
|
||||
|
||||
func newECBEncrypter(b cipher.Block) cipher.BlockMode {
|
||||
return (*ecbEncrypter)(newECB(b))
|
||||
}
|
||||
|
||||
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("crypto/cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("crypto/cipher: output smaller than input")
|
||||
}
|
||||
for len(src) > 0 {
|
||||
x.b.Encrypt(dst, src[:x.blockSize])
|
||||
src = src[x.blockSize:]
|
||||
dst = dst[x.blockSize:]
|
||||
}
|
||||
}
|
||||
|
||||
type ecbDecrypter ecb
|
||||
|
||||
func newECBDecrypter(b cipher.Block) cipher.BlockMode {
|
||||
return (*ecbDecrypter)(newECB(b))
|
||||
}
|
||||
|
||||
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
panic("crypto/cipher: input not full blocks")
|
||||
}
|
||||
if len(dst) < len(src) {
|
||||
panic("crypto/cipher: output smaller than input")
|
||||
}
|
||||
for len(src) > 0 {
|
||||
x.b.Decrypt(dst, src[:x.blockSize])
|
||||
src = src[x.blockSize:]
|
||||
dst = dst[x.blockSize:]
|
||||
}
|
||||
}
|
||||
65
pkg/lzkit/delay/ProgressiveDelay.go
Normal file
65
pkg/lzkit/delay/ProgressiveDelay.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package delay
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ProgressiveDelay 用于管理渐进式延迟策略
|
||||
type ProgressiveDelay struct {
|
||||
initialDelay time.Duration // 初始延迟时间
|
||||
growthFactor float64 // 延迟增长因子 (例如 1.5)
|
||||
maxDelay time.Duration // 最大延迟时间
|
||||
maxRetryDuration time.Duration // 最大重试时间
|
||||
currentDelay time.Duration // 当前延迟时间
|
||||
startTime time.Time // 重试开始时间
|
||||
}
|
||||
|
||||
// New 创建一个新的渐进式延迟对象
|
||||
func New(initialDelay, maxDelay, maxRetryDuration time.Duration, growthFactor float64) (*ProgressiveDelay, error) {
|
||||
// 参数校验
|
||||
if initialDelay <= 0 {
|
||||
return nil, errors.New("initialDelay must be greater than zero")
|
||||
}
|
||||
if maxDelay <= 0 {
|
||||
return nil, errors.New("maxDelay must be greater than zero")
|
||||
}
|
||||
if maxRetryDuration <= 0 {
|
||||
return nil, errors.New("maxRetryDuration must be greater than zero")
|
||||
}
|
||||
if growthFactor <= 1.0 {
|
||||
return nil, errors.New("growthFactor must be greater than 1")
|
||||
}
|
||||
|
||||
// 初始化并返回
|
||||
return &ProgressiveDelay{
|
||||
initialDelay: initialDelay,
|
||||
maxDelay: maxDelay,
|
||||
maxRetryDuration: maxRetryDuration,
|
||||
growthFactor: growthFactor,
|
||||
currentDelay: initialDelay,
|
||||
startTime: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NextDelay 计算并返回下次的延迟时间
|
||||
func (pd *ProgressiveDelay) NextDelay() (time.Duration, error) {
|
||||
// 检查最大重试时间是否已过
|
||||
if time.Since(pd.startTime) > pd.maxRetryDuration {
|
||||
return 0, fmt.Errorf("最大重试时间超过限制: %v", pd.maxRetryDuration)
|
||||
}
|
||||
|
||||
// 返回当前延迟时间
|
||||
delay := pd.currentDelay
|
||||
|
||||
// 计算下一个延迟时间并更新 currentDelay
|
||||
pd.currentDelay = time.Duration(float64(pd.currentDelay) * pd.growthFactor)
|
||||
|
||||
// 如果下次延迟超过最大延迟时间,限制在最大值
|
||||
if pd.currentDelay > pd.maxDelay {
|
||||
pd.currentDelay = pd.maxDelay
|
||||
}
|
||||
|
||||
return delay, nil
|
||||
}
|
||||
35
pkg/lzkit/lzUtils/json.go
Normal file
35
pkg/lzkit/lzUtils/json.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package lzUtils
|
||||
|
||||
import "github.com/bytedance/sonic"
|
||||
|
||||
func RecursiveParse(data interface{}) (interface{}, error) {
|
||||
switch v := data.(type) {
|
||||
case string:
|
||||
// 尝试解析字符串是否为嵌套 JSON
|
||||
var parsed interface{}
|
||||
if err := sonic.Unmarshal([]byte(v), &parsed); err == nil {
|
||||
return RecursiveParse(parsed)
|
||||
}
|
||||
return v, nil // 普通字符串直接返回
|
||||
case map[string]interface{}:
|
||||
for key, val := range v {
|
||||
parsed, err := RecursiveParse(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v[key] = parsed
|
||||
}
|
||||
return v, nil
|
||||
case []interface{}:
|
||||
for i, item := range v {
|
||||
parsed, err := RecursiveParse(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v[i] = parsed
|
||||
}
|
||||
return v, nil
|
||||
default:
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
72
pkg/lzkit/lzUtils/sqlutls.go
Normal file
72
pkg/lzkit/lzUtils/sqlutls.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package lzUtils
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// StringToNullString 将 string 转换为 sql.NullString
|
||||
func StringToNullString(s string) sql.NullString {
|
||||
return sql.NullString{
|
||||
String: s,
|
||||
Valid: s != "",
|
||||
}
|
||||
}
|
||||
|
||||
// NullStringToString 将 sql.NullString 转换为 string
|
||||
func NullStringToString(ns sql.NullString) string {
|
||||
if ns.Valid {
|
||||
return ns.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TimeToNullTime 将 time.Time 转换为 sql.NullTime
|
||||
func TimeToNullTime(t time.Time) sql.NullTime {
|
||||
return sql.NullTime{
|
||||
Time: t,
|
||||
Valid: !t.IsZero(), // 仅当 t 不是零值时才设置为有效
|
||||
}
|
||||
}
|
||||
|
||||
// NullTimeToTime 将 sql.NullTime 转换为 time.Time
|
||||
func NullTimeToTime(nt sql.NullTime) time.Time {
|
||||
if nt.Valid {
|
||||
return nt.Time
|
||||
}
|
||||
return time.Time{} // 返回零值时间
|
||||
}
|
||||
|
||||
// Int64ToNullInt64 将 int64 转换为 sql.NullInt64
|
||||
func Int64ToNullInt64(i int64) sql.NullInt64 {
|
||||
return sql.NullInt64{
|
||||
Int64: i,
|
||||
Valid: i != 0, // 仅当 i 非零时才设置为有效
|
||||
}
|
||||
}
|
||||
|
||||
// NullInt64ToInt64 将 sql.NullInt64 转换为 int64
|
||||
func NullInt64ToInt64(ni sql.NullInt64) int64 {
|
||||
if ni.Valid {
|
||||
return ni.Int64
|
||||
}
|
||||
return 0 // 返回零值 int64
|
||||
}
|
||||
|
||||
// Float64ToNullFloat64 将 float64 转换为 sql.NullFloat64
|
||||
// Valid 字段在 f 非零时设置为有效(注意:NaN 会被视为有效,需结合业务场景使用)
|
||||
func Float64ToNullFloat64(f float64) sql.NullFloat64 {
|
||||
return sql.NullFloat64{
|
||||
Float64: f,
|
||||
Valid: f != 0,
|
||||
}
|
||||
}
|
||||
|
||||
// NullFloat64ToFloat64 将 sql.NullFloat64 转换为 float64
|
||||
// 当 Valid 为 false 时,返回 float64 零值
|
||||
func NullFloat64ToFloat64(nf sql.NullFloat64) float64 {
|
||||
if nf.Valid {
|
||||
return nf.Float64
|
||||
}
|
||||
return 0.0
|
||||
}
|
||||
26
pkg/lzkit/lzUtils/time.go
Normal file
26
pkg/lzkit/lzUtils/time.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package lzUtils
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RenewMembership 延长会员有效期
|
||||
func RenewMembership(expiry sql.NullTime) sql.NullTime {
|
||||
// 确定基准时间
|
||||
var baseTime time.Time
|
||||
if expiry.Valid {
|
||||
baseTime = expiry.Time
|
||||
} else {
|
||||
baseTime = time.Now()
|
||||
}
|
||||
|
||||
// 增加一年(自动处理闰年)
|
||||
newTime := baseTime.AddDate(1, 0, 0)
|
||||
|
||||
// 返回始终有效的 NullTime
|
||||
return sql.NullTime{
|
||||
Time: newTime,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
15
pkg/lzkit/lzUtils/utils.go
Normal file
15
pkg/lzkit/lzUtils/utils.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package lzUtils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ToWechatAmount 将金额从元转换为微信支付 SDK 需要的分(int64 类型)
|
||||
func ToWechatAmount(amount float64) int64 {
|
||||
// 将金额从元转换为分,并四舍五入
|
||||
return int64(amount*100 + 0.5)
|
||||
}
|
||||
|
||||
// ToAlipayAmount 将金额从元转换为支付宝支付 SDK 需要的字符串格式,保留两位小数
|
||||
func ToAlipayAmount(amount float64) string {
|
||||
// 格式化为字符串,保留两位小数
|
||||
return fmt.Sprintf("%.2f", amount)
|
||||
}
|
||||
106
pkg/lzkit/md5/README.md
Normal file
106
pkg/lzkit/md5/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# MD5 工具包
|
||||
|
||||
这个包提供了全面的 MD5 哈希功能,包括字符串加密、文件加密、链式操作、加盐哈希等。
|
||||
|
||||
## 主要功能
|
||||
|
||||
- 字符串和字节切片的 MD5 哈希计算
|
||||
- 文件 MD5 哈希计算(支持大文件分块处理)
|
||||
- 链式 API,支持构建复杂的哈希内容
|
||||
- 带盐值的 MD5 哈希,提高安全性
|
||||
- 哈希验证功能
|
||||
- 16 位和 8 位 MD5 哈希(短版本)
|
||||
- HMAC-MD5 实现,增强安全性
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本使用
|
||||
|
||||
```go
|
||||
// 计算字符串的MD5哈希
|
||||
hash := md5.EncryptString("hello world")
|
||||
fmt.Println(hash) // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
|
||||
// 计算字节切片的MD5哈希
|
||||
bytes := []byte("hello world")
|
||||
hash = md5.EncryptBytes(bytes)
|
||||
|
||||
// 计算文件的MD5哈希
|
||||
fileHash, err := md5.EncryptFile("path/to/file.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(fileHash)
|
||||
```
|
||||
|
||||
### 链式 API
|
||||
|
||||
```go
|
||||
// 创建一个新的MD5实例并添加内容
|
||||
hash := md5.New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
fmt.Println(hash) // 5eb63bbbe01eeed093cb22bb8f5acdc3
|
||||
|
||||
// 或者从字符串初始化
|
||||
hash = md5.FromString("hello").
|
||||
Add(" world").
|
||||
Sum()
|
||||
|
||||
// 从字节切片初始化
|
||||
hash = md5.FromBytes([]byte("hello")).
|
||||
AddBytes([]byte(" world")).
|
||||
Sum()
|
||||
```
|
||||
|
||||
### 安全性增强
|
||||
|
||||
```go
|
||||
// 使用盐值加密(提高安全性)
|
||||
hashedPassword := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
|
||||
// 使用前缀加密
|
||||
hashedValue := md5.EncryptStringWithPrefix("secret-data", "prefix-")
|
||||
|
||||
// 验证带盐值的哈希
|
||||
isValid := md5.VerifyMD5WithSalt("password123", "main@example.com", hashedPassword)
|
||||
|
||||
// 使用HMAC-MD5提高安全性
|
||||
hmacHash := md5.MD5HMAC("message", "secret-key")
|
||||
```
|
||||
|
||||
### 短哈希值
|
||||
|
||||
```go
|
||||
// 获取16位MD5(32位MD5的中间部分)
|
||||
hash16 := md5.Get16("hello world")
|
||||
fmt.Println(hash16) // 中间16个字符
|
||||
|
||||
// 获取8位MD5
|
||||
hash8 := md5.Get8("hello world")
|
||||
fmt.Println(hash8) // 中间8个字符
|
||||
```
|
||||
|
||||
### 文件验证
|
||||
|
||||
```go
|
||||
// 验证文件MD5是否匹配
|
||||
match, err := md5.VerifyFileMD5("path/to/file.txt", "expected-hash")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if match {
|
||||
fmt.Println("文件MD5校验通过")
|
||||
} else {
|
||||
fmt.Println("文件MD5校验失败")
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. MD5 主要用于校验,不适合用于安全存储密码等敏感信息
|
||||
2. 如果用于密码存储,请务必使用加盐处理并考虑使用更安全的算法
|
||||
3. 处理大文件时请使用`EncryptFileChunk`以优化性能
|
||||
4. 返回的 MD5 哈希值都是 32 位的小写十六进制字符串(除非使用 Get16/Get8 函数)
|
||||
79
pkg/lzkit/md5/example_test.go
Normal file
79
pkg/lzkit/md5/example_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package md5_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hm-server/pkg/lzkit/md5"
|
||||
"log"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// 简单的字符串MD5
|
||||
hashValue := md5.EncryptString("hello world")
|
||||
fmt.Println("MD5(hello world):", hashValue)
|
||||
|
||||
// 使用链式API
|
||||
chainHash := md5.New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
fmt.Println("链式MD5:", chainHash)
|
||||
|
||||
// 使用盐值
|
||||
saltedHash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("加盐MD5:", saltedHash)
|
||||
|
||||
// 验证哈希
|
||||
isValid := md5.VerifyMD5("hello world", hashValue)
|
||||
fmt.Println("验证结果:", isValid)
|
||||
|
||||
// 生成短版本的MD5
|
||||
fmt.Println("16位MD5:", md5.Get16("hello world"))
|
||||
fmt.Println("8位MD5:", md5.Get8("hello world"))
|
||||
|
||||
// 文件MD5计算
|
||||
filePath := "example.txt" // 这只是示例,实际上这个文件可能不存在
|
||||
fileHash, err := md5.EncryptFile(filePath)
|
||||
if err != nil {
|
||||
// 在实际代码中执行正确的错误处理
|
||||
log.Printf("计算文件MD5出错: %v", err)
|
||||
} else {
|
||||
fmt.Println("文件MD5:", fileHash)
|
||||
}
|
||||
|
||||
// HMAC-MD5
|
||||
hmacHash := md5.MD5HMAC("重要消息", "secret-key")
|
||||
fmt.Println("HMAC-MD5:", hmacHash)
|
||||
}
|
||||
|
||||
func ExampleEncryptString() {
|
||||
hash := md5.EncryptString("HelloWorld")
|
||||
fmt.Println(hash)
|
||||
// Output: 68e109f0f40ca72a15e05cc22786f8e6
|
||||
}
|
||||
|
||||
func ExampleMD5_Sum() {
|
||||
hash := md5.New().
|
||||
Add("Hello").
|
||||
Add("World").
|
||||
Sum()
|
||||
fmt.Println(hash)
|
||||
// Output: 68e109f0f40ca72a15e05cc22786f8e6
|
||||
}
|
||||
|
||||
func ExampleEncryptStringWithSalt() {
|
||||
// 为用户密码加盐,通常使用用户唯一标识(如邮箱)作为盐值
|
||||
hash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("盐值哈希长度:", len(hash))
|
||||
fmt.Println("是否为有效哈希:", md5.VerifyMD5WithSalt("password123", "main@example.com", hash))
|
||||
// Output:
|
||||
// 盐值哈希长度: 32
|
||||
// 是否为有效哈希: true
|
||||
}
|
||||
|
||||
func ExampleGet16() {
|
||||
// 获取16位MD5,适合不需要完全防碰撞场景
|
||||
hash := md5.Get16("HelloWorld")
|
||||
fmt.Println(hash)
|
||||
// Output: f0f40ca72a15e05c
|
||||
}
|
||||
206
pkg/lzkit/md5/md5.go
Normal file
206
pkg/lzkit/md5/md5.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MD5结构体,可用于链式调用
|
||||
type MD5 struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
// New 创建一个新的MD5实例
|
||||
func New() *MD5 {
|
||||
return &MD5{
|
||||
data: []byte{},
|
||||
}
|
||||
}
|
||||
|
||||
// FromString 从字符串创建MD5
|
||||
func FromString(s string) *MD5 {
|
||||
return &MD5{
|
||||
data: []byte(s),
|
||||
}
|
||||
}
|
||||
|
||||
// FromBytes 从字节切片创建MD5
|
||||
func FromBytes(b []byte) *MD5 {
|
||||
return &MD5{
|
||||
data: b,
|
||||
}
|
||||
}
|
||||
|
||||
// Add 向MD5中添加字符串
|
||||
func (m *MD5) Add(s string) *MD5 {
|
||||
m.data = append(m.data, []byte(s)...)
|
||||
return m
|
||||
}
|
||||
|
||||
// AddBytes 向MD5中添加字节切片
|
||||
func (m *MD5) AddBytes(b []byte) *MD5 {
|
||||
m.data = append(m.data, b...)
|
||||
return m
|
||||
}
|
||||
|
||||
// Sum 计算并返回MD5哈希值(16进制字符串)
|
||||
func (m *MD5) Sum() string {
|
||||
hash := md5.New()
|
||||
hash.Write(m.data)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// SumBytes 计算并返回MD5哈希值(字节切片)
|
||||
func (m *MD5) SumBytes() []byte {
|
||||
hash := md5.New()
|
||||
hash.Write(m.data)
|
||||
return hash.Sum(nil)
|
||||
}
|
||||
|
||||
// 直接调用的工具函数
|
||||
|
||||
// EncryptString 加密字符串
|
||||
func EncryptString(s string) string {
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(s))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// EncryptBytes 加密字节切片
|
||||
func EncryptBytes(b []byte) string {
|
||||
hash := md5.New()
|
||||
hash.Write(b)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// EncryptFile 加密文件内容
|
||||
func EncryptFile(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// EncryptFileChunk 对大文件分块计算MD5,提高效率
|
||||
func EncryptFileChunk(filePath string, chunkSize int) (string, error) {
|
||||
if chunkSize <= 0 {
|
||||
chunkSize = 1024 * 1024 // 默认1MB
|
||||
}
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
buf := make([]byte, chunkSize)
|
||||
reader := bufio.NewReader(file)
|
||||
|
||||
for {
|
||||
n, err := reader.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
hash.Write(buf[:n])
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// EncryptStringWithSalt 使用盐值加密字符串
|
||||
func EncryptStringWithSalt(s, salt string) string {
|
||||
return EncryptString(s + salt)
|
||||
}
|
||||
|
||||
// EncryptStringWithPrefix 使用前缀加密字符串
|
||||
func EncryptStringWithPrefix(s, prefix string) string {
|
||||
return EncryptString(prefix + s)
|
||||
}
|
||||
|
||||
// VerifyMD5 验证字符串的MD5哈希是否匹配
|
||||
func VerifyMD5(s, hash string) bool {
|
||||
return EncryptString(s) == strings.ToLower(hash)
|
||||
}
|
||||
|
||||
// VerifyMD5WithSalt 验证带盐值的字符串MD5哈希是否匹配
|
||||
func VerifyMD5WithSalt(s, salt, hash string) bool {
|
||||
return EncryptStringWithSalt(s, salt) == strings.ToLower(hash)
|
||||
}
|
||||
|
||||
// VerifyFileMD5 验证文件的MD5哈希是否匹配
|
||||
func VerifyFileMD5(filePath, hash string) (bool, error) {
|
||||
fileHash, err := EncryptFile(filePath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return fileHash == strings.ToLower(hash), nil
|
||||
}
|
||||
|
||||
// MD5格式化为指定位数
|
||||
|
||||
// Get16 获取16位MD5值(取32位结果的中间16位)
|
||||
func Get16(s string) string {
|
||||
result := EncryptString(s)
|
||||
return result[8:24]
|
||||
}
|
||||
|
||||
// Get8 获取8位MD5值
|
||||
func Get8(s string) string {
|
||||
result := EncryptString(s)
|
||||
return result[12:20]
|
||||
}
|
||||
|
||||
// MD5主要用于校验而非安全存储,对于需要高安全性的场景,应考虑:
|
||||
// 1. bcrypt, scrypt或Argon2等专门为密码设计的算法
|
||||
// 2. HMAC-MD5等方式以防御彩虹表攻击
|
||||
// 3. 加盐并使用多次哈希迭代提高安全性
|
||||
|
||||
// MD5HMAC 使用HMAC-MD5算法
|
||||
func MD5HMAC(message, key string) string {
|
||||
hash := md5.New()
|
||||
|
||||
// 如果key长度超出block size,先进行哈希
|
||||
if len(key) > 64 {
|
||||
hash.Write([]byte(key))
|
||||
key = hex.EncodeToString(hash.Sum(nil))
|
||||
hash.Reset()
|
||||
}
|
||||
|
||||
// 内部填充
|
||||
k_ipad := make([]byte, 64)
|
||||
k_opad := make([]byte, 64)
|
||||
copy(k_ipad, []byte(key))
|
||||
copy(k_opad, []byte(key))
|
||||
|
||||
for i := 0; i < 64; i++ {
|
||||
k_ipad[i] ^= 0x36
|
||||
k_opad[i] ^= 0x5c
|
||||
}
|
||||
|
||||
// 内部哈希
|
||||
hash.Write(k_ipad)
|
||||
hash.Write([]byte(message))
|
||||
innerHash := hash.Sum(nil)
|
||||
hash.Reset()
|
||||
|
||||
// 外部哈希
|
||||
hash.Write(k_opad)
|
||||
hash.Write(innerHash)
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
192
pkg/lzkit/md5/md5_test.go
Normal file
192
pkg/lzkit/md5/md5_test.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncryptString(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"", "d41d8cd98f00b204e9800998ecf8427e"},
|
||||
{"hello", "5d41402abc4b2a76b9719d911017c592"},
|
||||
{"123456", "e10adc3949ba59abbe56e057f20f883e"},
|
||||
{"Hello World!", "ed076287532e86365e841e92bfc50d8c"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := EncryptString(test.input)
|
||||
fmt.Println(result)
|
||||
if result != test.expected {
|
||||
t.Errorf("EncryptString(%s) = %s; want %s", test.input, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
input []byte
|
||||
expected string
|
||||
}{
|
||||
{[]byte(""), "d41d8cd98f00b204e9800998ecf8427e"},
|
||||
{[]byte("hello"), "5d41402abc4b2a76b9719d911017c592"},
|
||||
{[]byte{0, 1, 2, 3, 4}, "5267768822ee624d48fce15ec5ca79b6"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := EncryptBytes(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("EncryptBytes(%v) = %s; want %s", test.input, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMD5Chain(t *testing.T) {
|
||||
// 测试链式调用
|
||||
result := New().
|
||||
Add("hello").
|
||||
Add(" ").
|
||||
Add("world").
|
||||
Sum()
|
||||
|
||||
expected := "fc5e038d38a57032085441e7fe7010b0" // MD5("hello world")
|
||||
if result != expected {
|
||||
t.Errorf("Chain MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
|
||||
// 测试从字符串初始化
|
||||
result = FromString("hello").Add(" world").Sum()
|
||||
if result != expected {
|
||||
t.Errorf("FromString MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
|
||||
// 测试从字节切片初始化
|
||||
result = FromBytes([]byte("hello")).AddBytes([]byte(" world")).Sum()
|
||||
if result != expected {
|
||||
t.Errorf("FromBytes MD5 = %s; want %s", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMD5(t *testing.T) {
|
||||
if !VerifyMD5("hello", "5d41402abc4b2a76b9719d911017c592") {
|
||||
t.Error("VerifyMD5 failed for correct match")
|
||||
}
|
||||
|
||||
if VerifyMD5("hello", "wrong-hash") {
|
||||
t.Error("VerifyMD5 succeeded for incorrect match")
|
||||
}
|
||||
|
||||
// 测试大小写不敏感
|
||||
if !VerifyMD5("hello", "5D41402ABC4B2A76B9719D911017C592") {
|
||||
t.Error("VerifyMD5 failed for uppercase hash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaltAndPrefix(t *testing.T) {
|
||||
// 测试加盐
|
||||
saltResult := EncryptStringWithSalt("password", "salt123")
|
||||
expectedSalt := EncryptString("passwordsalt123")
|
||||
if saltResult != expectedSalt {
|
||||
t.Errorf("EncryptStringWithSalt = %s; want %s", saltResult, expectedSalt)
|
||||
}
|
||||
|
||||
// 测试前缀
|
||||
prefixResult := EncryptStringWithPrefix("password", "prefix123")
|
||||
expectedPrefix := EncryptString("prefix123password")
|
||||
if prefixResult != expectedPrefix {
|
||||
t.Errorf("EncryptStringWithPrefix = %s; want %s", prefixResult, expectedPrefix)
|
||||
}
|
||||
|
||||
// 验证带盐值的MD5
|
||||
if !VerifyMD5WithSalt("password", "salt123", saltResult) {
|
||||
t.Error("VerifyMD5WithSalt failed for correct match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet16And8(t *testing.T) {
|
||||
full := EncryptString("test-string")
|
||||
|
||||
// 测试16位MD5
|
||||
result16 := Get16("test-string")
|
||||
expected16 := full[8:24]
|
||||
if result16 != expected16 {
|
||||
t.Errorf("Get16 = %s; want %s", result16, expected16)
|
||||
}
|
||||
|
||||
// 测试8位MD5
|
||||
result8 := Get8("test-string")
|
||||
expected8 := full[12:20]
|
||||
if result8 != expected8 {
|
||||
t.Errorf("Get8 = %s; want %s", result8, expected8)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMD5HMAC(t *testing.T) {
|
||||
// 已知的HMAC-MD5结果
|
||||
tests := []struct {
|
||||
message string
|
||||
key string
|
||||
expected string
|
||||
}{
|
||||
{"message", "key", "4e4748e62b463521f6775fbf921234b5"},
|
||||
{"test", "secret", "8b11d99898918564dda1a9fe205b5310"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := MD5HMAC(test.message, test.key)
|
||||
if result != test.expected {
|
||||
t.Errorf("MD5HMAC(%s, %s) = %s; want %s",
|
||||
test.message, test.key, result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptFile(t *testing.T) {
|
||||
// 创建临时测试文件
|
||||
content := []byte("test file content for MD5")
|
||||
tmpFile, err := os.CreateTemp("", "md5test-*.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建临时文件: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
if _, err := tmpFile.Write(content); err != nil {
|
||||
t.Fatalf("无法写入临时文件: %v", err)
|
||||
}
|
||||
if err := tmpFile.Close(); err != nil {
|
||||
t.Fatalf("无法关闭临时文件: %v", err)
|
||||
}
|
||||
|
||||
// 计算文件MD5
|
||||
fileHash, err := EncryptFile(tmpFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("计算文件MD5失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证文件MD5
|
||||
expectedHash := EncryptBytes(content)
|
||||
if fileHash != expectedHash {
|
||||
t.Errorf("文件MD5 = %s; 应为 %s", fileHash, expectedHash)
|
||||
}
|
||||
|
||||
// 测试VerifyFileMD5
|
||||
match, err := VerifyFileMD5(tmpFile.Name(), expectedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("验证文件MD5失败: %v", err)
|
||||
}
|
||||
if !match {
|
||||
t.Error("VerifyFileMD5返回false,应返回true")
|
||||
}
|
||||
|
||||
// 测试不匹配的情况
|
||||
match, err = VerifyFileMD5(tmpFile.Name(), "wronghash")
|
||||
if err != nil {
|
||||
t.Fatalf("验证文件MD5失败: %v", err)
|
||||
}
|
||||
if match {
|
||||
t.Error("VerifyFileMD5对错误的哈希返回true,应返回false")
|
||||
}
|
||||
}
|
||||
38
pkg/lzkit/validator/error_messages.go
Normal file
38
pkg/lzkit/validator/error_messages.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package validator
|
||||
|
||||
// 定义自定义错误消息
|
||||
var customMessages = map[string]string{
|
||||
"Name.min": "姓名不能少于1个字",
|
||||
"Name.required": "姓名是必填项",
|
||||
"Name.name": "姓名只能包含中文",
|
||||
"NameMan.min": "男方姓名不能少于1个字",
|
||||
"NameMan.required": "男方姓名是必填项",
|
||||
"NameMan.name": "男方姓名只能包含中文",
|
||||
"NameWoman.min": "女方姓名不能少于1个字",
|
||||
"NameWoman.required": "女方姓名是必填项",
|
||||
"NameWoman.name": "女方姓名只能包含中文",
|
||||
"Mobile.required": "手机号是必填项",
|
||||
"Mobile.min": "电话号码必须为有效的中国电话号码",
|
||||
"Mobile.max": "电话号码必须为有效的中国电话号码",
|
||||
"Mobile.mobile": "电话号码必须为有效的中国电话号码",
|
||||
"IDCard.required": "身份证号是必填项",
|
||||
"IDCard.idCard": "无效的身份证号码",
|
||||
"IDCardMan.required": "男方身份证号是必填项",
|
||||
"IDCardMan.idCard": "无效的男方身份证号码",
|
||||
"IDCardWoman.required": "女方身份证号是必填项",
|
||||
"IDCardWoman.idCard": "无效的女方身份证号码",
|
||||
"Password.min": "密码不能少于8位数",
|
||||
"Password.max": "密码不能超过32位数",
|
||||
"Password.password": "密码强度太弱",
|
||||
//"EntCode.required":"请输入统一社会信用代码",
|
||||
//"EntCode.USCI": "请输入正确的统一社会信用代码",
|
||||
}
|
||||
|
||||
// 获取自定义错误消息
|
||||
func GetErrorMessage(field, tag string) string {
|
||||
key := field + "." + tag
|
||||
if msg, exists := customMessages[key]; exists {
|
||||
return msg
|
||||
}
|
||||
return "请输入正确格式的参数"
|
||||
}
|
||||
174
pkg/lzkit/validator/validator.go
Normal file
174
pkg/lzkit/validator/validator.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var validate *validator.Validate
|
||||
|
||||
// 初始化自定义校验器
|
||||
func init() {
|
||||
validate = validator.New()
|
||||
|
||||
if err := validate.RegisterValidation("name", validName); err != nil {
|
||||
panic(fmt.Sprintf("注册 name 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
// 注册自定义验证器 validmobile
|
||||
if err := validate.RegisterValidation("mobile", validmobile); err != nil {
|
||||
panic(fmt.Sprintf("注册 mobile 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
// 注册自定义验证器 validDate
|
||||
if err := validate.RegisterValidation("date", validDate); err != nil {
|
||||
panic(fmt.Sprintf("注册 date 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
// 注册自定义验证器 validIDCard
|
||||
if err := validate.RegisterValidation("idCard", validIDCard); err != nil {
|
||||
panic(fmt.Sprintf("注册 idCard 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
if err := validate.RegisterValidation("bankCard", validBankCard); err != nil {
|
||||
panic(fmt.Sprintf("注册 bankCard 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
if err := validate.RegisterValidation("USCI", validUSCI); err != nil {
|
||||
panic(fmt.Sprintf("注册 USCI 社会统一信用代码 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
if err := validate.RegisterValidation("mobileType", validMobileType); err != nil {
|
||||
panic(fmt.Sprintf("注册 mobileType 验证器时发生错误: %v", err))
|
||||
}
|
||||
if err := validate.RegisterValidation("password", validatePassword); err != nil {
|
||||
panic(fmt.Sprintf("注册 password 验证器时发生错误: %v", err))
|
||||
}
|
||||
if err := validate.RegisterValidation("payMethod", validatePayMethod); err != nil {
|
||||
panic(fmt.Sprintf("注册 payMethod 验证器时发生错误: %v", err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 弱口令列表
|
||||
var weakPasswords = []string{
|
||||
"12345678", "password", "123456789", "qwerty", "123456", "letmein",
|
||||
"1234567", "welcome", "abc123", "password1", "1234", "111111", "admin",
|
||||
}
|
||||
|
||||
// Validate 校验参数逻辑
|
||||
func Validate(req interface{}) error {
|
||||
if err := validate.Struct(req); err != nil {
|
||||
// 检查 err 是否是 ValidationErrors 类型
|
||||
if validationErrors, ok := err.(validator.ValidationErrors); ok {
|
||||
for _, validationErr := range validationErrors {
|
||||
field := validationErr.StructField()
|
||||
tag := validationErr.Tag()
|
||||
return errors.New(GetErrorMessage(field, tag))
|
||||
}
|
||||
} else {
|
||||
// 其他错误处理
|
||||
return fmt.Errorf("验证时出现未知错误: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 自定义的名称验证
|
||||
func validName(fl validator.FieldLevel) bool {
|
||||
name := fl.Field().String()
|
||||
validNamePattern := `^[\p{Han}]+$`
|
||||
matched, _ := regexp.MatchString(validNamePattern, name)
|
||||
return matched
|
||||
}
|
||||
|
||||
// 自定义的手机号验证
|
||||
func validmobile(fl validator.FieldLevel) bool {
|
||||
phone := fl.Field().String()
|
||||
validmobilePattern := `^1[3-9]\d{9}$`
|
||||
matched, _ := regexp.MatchString(validmobilePattern, phone)
|
||||
return matched
|
||||
}
|
||||
|
||||
// 自定义正则表达式校验 yyyyMMdd 格式
|
||||
func validDate(fl validator.FieldLevel) bool {
|
||||
date := fl.Field().String()
|
||||
validDatePattern := `^\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$`
|
||||
matched, _ := regexp.MatchString(validDatePattern, date)
|
||||
return matched
|
||||
}
|
||||
|
||||
// 自定义身份证校验
|
||||
func validIDCard(fl validator.FieldLevel) bool {
|
||||
id := fl.Field().String()
|
||||
validIDPattern := `^\d{17}(\d|X|x)$` // 匹配18位身份证号码
|
||||
matched, _ := regexp.MatchString(validIDPattern, id)
|
||||
return matched
|
||||
}
|
||||
|
||||
func validBankCard(fl validator.FieldLevel) bool {
|
||||
bankCard := fl.Field().String()
|
||||
// 银行卡号一般是13到19位的数字
|
||||
validBankCardPattern := `^\d{13,19}$`
|
||||
matched, _ := regexp.MatchString(validBankCardPattern, bankCard)
|
||||
return matched
|
||||
}
|
||||
|
||||
func validUSCI(fl validator.FieldLevel) bool {
|
||||
usci := fl.Field().String()
|
||||
// 社会信用代码为18位数字和大写字母的组合,最后一位为校验码
|
||||
validUSCIPattern := `^[1-9A-Z]{2}[0-9]{6}[0-9A-Z]{9}[0-9A-Z]$`
|
||||
matched, _ := regexp.MatchString(validUSCIPattern, usci)
|
||||
return matched
|
||||
}
|
||||
|
||||
// 自定义的手机号类型验证(可以为空)
|
||||
func validMobileType(fl validator.FieldLevel) bool {
|
||||
mobileType := fl.Field().String()
|
||||
if mobileType == "" {
|
||||
return true // 如果为空,认为是有效的
|
||||
}
|
||||
|
||||
// 校验是否是 CTCC, CMCC, CUCC 之一
|
||||
validTypes := map[string]bool{
|
||||
"CTCC": true, // 中国电信
|
||||
"CMCC": true, // 中国移动
|
||||
"CUCC": true, // 中国联通
|
||||
}
|
||||
|
||||
return validTypes[mobileType]
|
||||
}
|
||||
|
||||
// 自定义密码强度校验函数
|
||||
func validatePassword(fl validator.FieldLevel) bool {
|
||||
password := fl.Field().String()
|
||||
|
||||
// 检查密码是否在弱口令列表中
|
||||
for _, weakPwd := range weakPasswords {
|
||||
if strings.ToLower(password) == weakPwd {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 支付方式
|
||||
func validatePayMethod(fl validator.FieldLevel) bool {
|
||||
payMethod := fl.Field().String()
|
||||
|
||||
if payMethod == "" {
|
||||
return true // 如果为空,认为是有效的
|
||||
}
|
||||
|
||||
validTypes := map[string]bool{
|
||||
"alipay": true, // 中国电信
|
||||
"wechatpay": true, // 中国移动
|
||||
}
|
||||
|
||||
return validTypes[payMethod]
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user