-
Notifications
You must be signed in to change notification settings - Fork 163
/
bcrypt.go
30 lines (25 loc) · 686 Bytes
/
bcrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package hashing
import (
"golang.org/x/crypto/bcrypt"
)
type bcryptHasher struct {
cost int
}
func NewBcryptHashComparer(cost int) HashComparer {
return bcryptHasher{
cost: cost,
}
}
// Hash generates a hashed password using bcrypt.
func (h bcryptHasher) Hash(password string) (string, error) {
generated, err := bcrypt.GenerateFromPassword([]byte(password), h.cost)
return string(generated), err
}
// Compare checks that a bcrypt generated password matches the password hash.
func (h bcryptHasher) Compare(password, passwordHash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password))
if err != nil {
return false
}
return true
}