-
Notifications
You must be signed in to change notification settings - Fork 69
/
bcrypt.go
45 lines (38 loc) · 902 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
package dongle
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
type BcryptError struct {
}
func NewBcryptError() BcryptError {
return BcryptError{}
}
func (e BcryptError) RoundsError() error {
return fmt.Errorf("bcrypt: invalid rounds, the rounds at least 4 and at most 31")
}
// ByBcrypt signs by bcrypt.
func (s Signer) ByBcrypt(rounds ...int) Signer {
if len(s.src) == 0 || s.Error != nil {
return s
}
if len(rounds) == 0 {
// 10 is the standard number of rounds in bcrypt.
rounds = []int{10}
}
dst, err := bcrypt.GenerateFromPassword(s.src, rounds[0])
if rounds[0] < 4 || rounds[0] > 31 || err != nil {
s.Error = NewBcryptError().RoundsError()
return s
}
s.dst = dst
return s
}
// ByBcrypt verify by bcrypt.
func (v Verifier) ByBcrypt() Verifier {
if len(v.src) == 0 || v.Error != nil {
return v
}
v.Error = bcrypt.CompareHashAndPassword(v.sign, v.src)
return v
}