31 lines
719 B
Go
31 lines
719 B
Go
package bcrypt
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// Verifier verifys passwords using Bcrypt.
|
|
type Verifier struct{}
|
|
|
|
// Verify verifys a Password.
|
|
func (bv *Verifier) Verify(password string, hashOnDB string) error {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashOnDB), []byte(password))
|
|
if err != nil {
|
|
return fmt.Errorf("verify password: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Hash calculates a hash to be stored on the database.
|
|
func (bv *Verifier) Hash(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", fmt.Errorf("hash password: %w", err)
|
|
}
|
|
|
|
return string(hash[:]), nil
|
|
}
|