31 lines
614 B
Go
31 lines
614 B
Go
|
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
|
||
|
}
|