/
pbkdf2.go
58 lines (49 loc) · 980 Bytes
/
pbkdf2.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
package pbkdf2
import (
"crypto/sha512"
"encoding/hex"
"hash"
"golang.org/x/crypto/pbkdf2"
)
const (
// see: https://godoc.org/golang.org/x/crypto/pbkdf2
defaultIterationSize = 4096
defaultKeyLength = 32
)
var (
defaultHashFn = sha512.New
)
// PBKDF2 is struct to create hash.
type PBKDF2 struct {
HashFn func() hash.Hash
IterationSize int
KeyLength int
}
// Hash creates hased text from password.
func (p PBKDF2) Hash(password, salt string) string {
return hex.EncodeToString(pbkdf2.Key(
[]byte(password),
[]byte(salt),
p.getIterationSize(),
p.getKeyLength(),
p.getHashFn(),
))
}
func (p PBKDF2) getHashFn() func() hash.Hash {
if p.HashFn == nil {
return defaultHashFn
}
return p.HashFn
}
func (p PBKDF2) getIterationSize() int {
if p.IterationSize == 0 {
return defaultIterationSize
}
return p.IterationSize
}
func (p PBKDF2) getKeyLength() int {
if p.KeyLength == 0 {
return defaultKeyLength
}
return p.KeyLength
}