后台面板
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
|
||||
}
|
||||
@@ -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