-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbkdf2.go
More file actions
34 lines (28 loc) · 743 Bytes
/
Copy pathpbkdf2.go
File metadata and controls
34 lines (28 loc) · 743 Bytes
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
package kdfcrypt
import (
"fmt"
"golang.org/x/crypto/pbkdf2"
)
// PBKDF2 params.
type PBKDF2 struct {
Iteration uint32 `param:"iter"`
HashFunc string `param:"hash"`
}
// SetDefaultParam sets the default param for PBKDF2.
func (kdf *PBKDF2) SetDefaultParam() {
if kdf.Iteration == 0 {
kdf.Iteration = 1024
}
if kdf.HashFunc == "" {
kdf.HashFunc = "sha512"
}
}
// Derive hash with PBKDF2.
func (kdf *PBKDF2) Derive(password, salt []byte, hashLength uint32) ([]byte, error) {
hashFunc, ok := hashFuncMap[kdf.HashFunc]
if !ok {
return nil, fmt.Errorf("Hash func for PBKDF2 is not valid: %s", kdf.HashFunc)
}
hashed := pbkdf2.Key([]byte(password), salt, int(kdf.Iteration), int(hashLength), hashFunc)
return hashed, nil
}