You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
449 B
18 lines
449 B
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
|
|
}
|