⚗️ add bcrypt
This commit is contained in:
20
utils/encrypt.go
Normal file
20
utils/encrypt.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package utils
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// Encrypt 加密
|
||||
func Encrypt(val string) (string, error) {
|
||||
// 使用bcrypt库的GenerateFromPassword函数进行哈希处理
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(val), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hashedPassword), err
|
||||
}
|
||||
|
||||
// Verify 验证
|
||||
func Verify(hashedVal, val string) bool {
|
||||
// 使用bcrypt库的CompareHashAndPassword函数比较密码
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hashedVal), []byte(val))
|
||||
return err == nil
|
||||
}
|
Reference in New Issue
Block a user