BrainMinder/internal/password/hash.go

31 lines
614 B
Go
Raw Normal View History

2024-08-22 10:13:16 +02:00
package password
import (
"errors"
"golang.org/x/crypto/bcrypt"
)
func Hash(plaintextPassword string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(plaintextPassword), 12)
if err != nil {
return "", err
}
return string(hashedPassword), nil
}
func Matches(plaintextPassword, hashedPassword string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(plaintextPassword))
if err != nil {
switch {
case errors.Is(err, bcrypt.ErrMismatchedHashAndPassword):
return false, nil
default:
return false, err
}
}
return true, nil
}