-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcrypt.go
49 lines (39 loc) · 997 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// +build go1.11
package passwd
import "golang.org/x/crypto/bcrypt"
var (
bcryptCommonParameters = BcryptParams{Cost: bcrypt.DefaultCost}
bcryptParanoidParameters = BcryptParams{Cost: bcrypt.MaxCost}
)
const (
idBcrypt = "2a"
)
// BcryptParams are the parameters for the bcrypt key derivation.
type BcryptParams struct {
Cost int
Masked bool // XXX UNUSED
}
func newBcryptParamsFromHash(hashed []byte) (*BcryptParams, error) {
hashCost, err := bcrypt.Cost(hashed)
if err != nil {
return nil, err
}
bp := BcryptParams{
Cost: hashCost,
}
return &bp, nil
}
func (bp *BcryptParams) generateFromPassword(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, bp.Cost)
}
func (bp *BcryptParams) compare(hashed, password []byte) error {
hashCost, err := bcrypt.Cost(hashed)
if err != nil || hashCost != bp.Cost {
return ErrMismatch
}
err = bcrypt.CompareHashAndPassword(hashed, password)
if err != nil {
return ErrMismatch
}
return nil
}