28 lines
637 B
Go
28 lines
637 B
Go
package bcrypt
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"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 {
|
|
return bcrypt.CompareHashAndPassword([]byte(hashOnDb), []byte(password))
|
|
}
|
|
|
|
// 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 "", err
|
|
}
|
|
idx := bytes.IndexByte(hash, 0)
|
|
return string(hash[:idx]), nil
|
|
}
|