first commit

This commit is contained in:
2025-09-21 18:27:25 +08:00
commit 02e1c4bd63
528 changed files with 55690 additions and 0 deletions

View 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相关测试通过
✅ 性能测试完成
✅ 密码生成功能正常
✅ 验证功能正常