budgeteer/bcrypt/verifier.go
2022-02-19 22:04:35 +00:00

33 lines
722 B
Go

package bcrypt
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
// Verifier verifys passwords using Bcrypt.
type Verifier struct {
cost int
}
// 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), bv.cost)
if err != nil {
return "", fmt.Errorf("hash password: %w", err)
}
return string(hash[:]), nil
}