mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 23:43:45 +08:00
30 lines
701 B
Go
30 lines
701 B
Go
package util
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// HashPassword 密码哈希
|
|
func HashPassword(password string) (string, error) {
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hashedPassword), nil
|
|
}
|
|
|
|
// PasswordOK 检查密码
|
|
func PasswordOK(hashedPassword, password string) bool {
|
|
if hashedPassword == "" && password == "" {
|
|
return true
|
|
}
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
// IsHashedPassword 是否是哈希密码
|
|
func IsHashedPassword(password string) bool {
|
|
_, err := bcrypt.Cost([]byte(password))
|
|
return err == nil
|
|
}
|