golang-lib/web/auth/lib_password.go

24 lines
613 B
Go
Raw Permalink Normal View History

2021-06-22 18:17:52 +02:00
package auth
import (
"golang.org/x/crypto/bcrypt"
)
2021-09-11 11:15:38 +02:00
// PasswordHashCost - to set global, for more speed or security
var PasswordHashCost = bcrypt.DefaultCost
2021-06-22 18:17:52 +02:00
// HashPassword - create new hash of password
func HashPassword(password string) (string, error) {
p, err := bcrypt.GenerateFromPassword([]byte(password), PasswordHashCost)
2021-06-22 18:17:52 +02:00
if err != nil {
return "", err
}
return string(p), nil
}
// ValidatePassword - check if given password is equal to saved hash
func ValidatePassword(hash, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}