25 lines
512 B
Go
25 lines
512 B
Go
package auth
|
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
func HashPassword(plain string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(hash), nil
|
|
}
|
|
|
|
func ComparePassword(hash string, plain string) (bool, error) {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain))
|
|
if err != nil {
|
|
if err == bcrypt.ErrMismatchedHashAndPassword {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|