24 lines
528 B
Go
24 lines
528 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
hashPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", fmt.Errorf("加密失败:%v", err)
|
|
}
|
|
return string(hashPassword), nil
|
|
}
|
|
|
|
func ValidPassword(hashPassword, password string) error {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashPassword), []byte(password))
|
|
if err != nil {
|
|
return fmt.Errorf("密码不匹配:%v", err)
|
|
}
|
|
return nil
|
|
}
|