-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathhasher.go
More file actions
33 lines (26 loc) · 831 Bytes
/
hasher.go
File metadata and controls
33 lines (26 loc) · 831 Bytes
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
31
32
33
package authboss
import (
"golang.org/x/crypto/bcrypt"
)
// Hasher is the interface that wraps the hashing and comparison of passwords
type Hasher interface {
CompareHashAndPassword(hash, password string) error
GenerateHash(password string) (string, error)
}
// NewBCryptHasher creates a new bcrypt hasher with the given cost
func NewBCryptHasher(cost int) *bcryptHasher {
return &bcryptHasher{cost: cost}
}
type bcryptHasher struct {
cost int
}
func (h *bcryptHasher) GenerateHash(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), h.cost)
if err != nil {
return "", err
}
return string(hash), nil
}
func (h *bcryptHasher) CompareHashAndPassword(hashedPassword, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}