-
Notifications
You must be signed in to change notification settings - Fork 25
/
kdf_scrypt.go
67 lines (55 loc) · 1.52 KB
/
kdf_scrypt.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
package pkcs8
import (
"encoding/asn1"
"golang.org/x/crypto/scrypt"
)
var (
oidScrypt = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11591, 4, 11}
)
// scrypt 数据
type scryptParams struct {
Salt []byte
CostParameter int
BlockSize int
ParallelizationParameter int
}
func (this scryptParams) DeriveKey(password []byte, size int) (key []byte, err error) {
return scrypt.Key(
password, this.Salt,
this.CostParameter, this.BlockSize,
this.ParallelizationParameter, size,
)
}
// ScryptOpts 设置
type ScryptOpts struct {
SaltSize int
CostParameter int
BlockSize int
ParallelizationParameter int
}
func (this ScryptOpts) DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error) {
key, err = scrypt.Key(
password, salt,
this.CostParameter, this.BlockSize,
this.ParallelizationParameter, size,
)
if err != nil {
return nil, nil, err
}
params = scryptParams{
BlockSize: this.BlockSize,
CostParameter: this.CostParameter,
ParallelizationParameter: this.ParallelizationParameter,
Salt: salt,
}
return key, params, nil
}
func (this ScryptOpts) GetSaltSize() int {
return this.SaltSize
}
func (this ScryptOpts) OID() asn1.ObjectIdentifier {
return oidScrypt
}
func init() {
AddKDF(oidScrypt, new(scryptParams))
}