26 lines
589 B
Go
26 lines
589 B
Go
package utils
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const (
|
|
// bcrypt 密钥成本
|
|
bcryptCost = bcrypt.DefaultCost
|
|
)
|
|
|
|
// HashPassword 对密码进行哈希加密
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
// VerifyPassword 验证密码是否正确
|
|
func VerifyPassword(hashedPassword, password string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
return err == nil
|
|
}
|