24 lines
520 B
Go
24 lines
520 B
Go
package identity
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func ValidatePassword(_ string, password string) error {
|
|
if len([]rune(password)) < 6 {
|
|
return fmt.Errorf("密码至少需要 6 个字符")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(hash), err
|
|
}
|
|
|
|
func VerifyPassword(hash, password string) bool {
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
|
}
|