forked from youmark/pkcs8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdf_scrypt.go
62 lines (51 loc) · 1.4 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
package pkcs8
import (
"encoding/asn1"
"golang.org/x/crypto/scrypt"
)
var (
oidScrypt = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11591, 4, 11}
)
func init() {
RegisterKDF(oidScrypt, func() KDFParameters {
return new(scryptParams)
})
}
type scryptParams struct {
Salt []byte
CostParameter int
BlockSize int
ParallelizationParameter int
}
func (p scryptParams) DeriveKey(password []byte, size int) (key []byte, err error) {
return scrypt.Key(password, p.Salt, p.CostParameter, p.BlockSize,
p.ParallelizationParameter, size)
}
// ScryptOpts contains options for the scrypt key derivation function.
type ScryptOpts struct {
SaltSize int
CostParameter int
BlockSize int
ParallelizationParameter int
}
func (p ScryptOpts) DeriveKey(password, salt []byte, size int) (
key []byte, params KDFParameters, err error) {
key, err = scrypt.Key(password, salt, p.CostParameter, p.BlockSize,
p.ParallelizationParameter, size)
if err != nil {
return nil, nil, err
}
params = scryptParams{
BlockSize: p.BlockSize,
CostParameter: p.CostParameter,
ParallelizationParameter: p.ParallelizationParameter,
Salt: salt,
}
return key, params, nil
}
func (p ScryptOpts) GetSaltSize() int {
return p.SaltSize
}
func (p ScryptOpts) OID() asn1.ObjectIdentifier {
return oidScrypt
}