liujiandao
2024-03-26 6b373cf70da0903a44ae3d7c4ebebae7c99dc2e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package utils
 
import (
    "golang.org/x/crypto/bcrypt"
)
 
// BcryptHash 使用 bcrypt 对密码进行加密
func BcryptHash(password string) string {
    bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    return string(bytes)
}
 
// BcryptCheck 对比明文密码和数据库的哈希值
func BcryptCheck(password, hash string) bool {
    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
    return err == nil
}