新增后台管理
This commit is contained in:
@@ -172,7 +172,7 @@ func hexEncodingExample() {
|
||||
|
||||
```yaml
|
||||
# etc/main.yaml
|
||||
Name: user-api
|
||||
Name: main-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
func TestAesEcbMobileEncryption(t *testing.T) {
|
||||
// 测试手机号加密
|
||||
mobile := "13800138000"
|
||||
key := []byte("1234567890abcdef") // 16字节AES-128密钥
|
||||
mobile := "13280082033 "
|
||||
key := []byte("ff83609b2b24fc73196aac3d3dfb874f") // 16字节AES-128密钥
|
||||
|
||||
keyStr := hex.EncodeToString(key)
|
||||
// 测试加密
|
||||
@@ -18,13 +18,14 @@ func TestAesEcbMobileEncryption(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("手机号加密失败: %v", err)
|
||||
}
|
||||
fmt.Println(encrypted)
|
||||
fmt.Printf("encrypted: %s\n", encrypted)
|
||||
jmStr := "oEpLcrIpDPN63rOlESXTDg=="
|
||||
// 测试解密
|
||||
decrypted, err := DecryptMobile(encrypted, keyStr)
|
||||
decrypted, err := DecryptMobile(jmStr, keyStr)
|
||||
if err != nil {
|
||||
t.Fatalf("手机号解密失败: %v", err)
|
||||
}
|
||||
fmt.Println(decrypted)
|
||||
fmt.Printf("decrypted: %s\n", decrypted)
|
||||
// 验证结果
|
||||
if decrypted != mobile {
|
||||
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", mobile, decrypted)
|
||||
|
||||
@@ -59,13 +59,13 @@ hash = md5.FromBytes([]byte("hello")).
|
||||
|
||||
```go
|
||||
// 使用盐值加密(提高安全性)
|
||||
hashedPassword := md5.EncryptStringWithSalt("password123", "user@example.com")
|
||||
hashedPassword := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
|
||||
// 使用前缀加密
|
||||
hashedValue := md5.EncryptStringWithPrefix("secret-data", "prefix-")
|
||||
|
||||
// 验证带盐值的哈希
|
||||
isValid := md5.VerifyMD5WithSalt("password123", "user@example.com", hashedPassword)
|
||||
isValid := md5.VerifyMD5WithSalt("password123", "main@example.com", hashedPassword)
|
||||
|
||||
// 使用HMAC-MD5提高安全性
|
||||
hmacHash := md5.MD5HMAC("message", "secret-key")
|
||||
|
||||
@@ -20,7 +20,7 @@ func Example() {
|
||||
fmt.Println("链式MD5:", chainHash)
|
||||
|
||||
// 使用盐值
|
||||
saltedHash := md5.EncryptStringWithSalt("password123", "user@example.com")
|
||||
saltedHash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("加盐MD5:", saltedHash)
|
||||
|
||||
// 验证哈希
|
||||
@@ -63,9 +63,9 @@ func ExampleMD5_Sum() {
|
||||
|
||||
func ExampleEncryptStringWithSalt() {
|
||||
// 为用户密码加盐,通常使用用户唯一标识(如邮箱)作为盐值
|
||||
hash := md5.EncryptStringWithSalt("password123", "user@example.com")
|
||||
hash := md5.EncryptStringWithSalt("password123", "main@example.com")
|
||||
fmt.Println("盐值哈希长度:", len(hash))
|
||||
fmt.Println("是否为有效哈希:", md5.VerifyMD5WithSalt("password123", "user@example.com", hash))
|
||||
fmt.Println("是否为有效哈希:", md5.VerifyMD5WithSalt("password123", "main@example.com", hash))
|
||||
// Output:
|
||||
// 盐值哈希长度: 32
|
||||
// 是否为有效哈希: true
|
||||
|
||||
Reference in New Issue
Block a user