-
Notifications
You must be signed in to change notification settings - Fork 24
/
kdf_bcrypt.go
74 lines (59 loc) · 1.36 KB
/
kdf_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package ssh
import (
"golang.org/x/crypto/ssh"
"github.com/deatil/go-cryptobin/kdf/bcrypt_pbkdf"
)
var (
bcryptName = "bcrypt"
)
// 设置
type bcryptOpts struct {
Salt string
Rounds uint32
}
// bcrypt 数据
type bcryptParams struct {}
func (this bcryptParams) DeriveKey(password []byte, kdfOpts string, size int) ([]byte, error) {
var opts bcryptOpts
if err := ssh.Unmarshal([]byte(kdfOpts), &opts); err != nil {
return nil, err
}
return bcrypt_pbkdf.Key(
password, []byte(opts.Salt),
int(opts.Rounds), size,
)
}
// BcryptOpts 设置
type BcryptOpts struct {
SaltSize int
Rounds int
}
func (this BcryptOpts) DeriveKey(password []byte, size int) ([]byte, string, error) {
salt, err := genRandom(this.SaltSize)
if err != nil {
return nil, "", err
}
key, err := bcrypt_pbkdf.Key(
password, salt,
this.Rounds, size,
)
if err != nil {
return nil, "", err
}
params := ssh.Marshal(bcryptOpts{
Salt: string(salt),
Rounds: uint32(this.Rounds),
})
return key, string(params), nil
}
func (this BcryptOpts) GetSaltSize() int {
return this.SaltSize
}
func (this BcryptOpts) Name() string {
return bcryptName
}
func init() {
AddKDF(bcryptName, func() KDFParameters {
return new(bcryptParams)
})
}