/
kdfutil.go
33 lines (29 loc) · 974 Bytes
/
kdfutil.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
package kdfutil
import (
"hash"
"time"
"code.google.com/p/go.crypto/pbkdf2"
)
const maxInt = int(^uint(0) >> 1)
// BUG: clocks are not monotonic.
// CalibratePBKDF2 returns the estimated number of PBKDF2 iterations required
// to derive key of length keyLen with hash function h in the given time.
func CalibratePBKDF2(dur time.Duration, h func() hash.Hash, keyLen int) int {
// XXX uses block size as input, maybe provide password and salt len?
block := make([]byte, h().BlockSize())
start := time.Now()
var ns int64
iter := 0
for {
pbkdf2.Key(block, block, 1000, keyLen, h)
iter += 1000
ns = time.Since(start).Nanoseconds()
if ns > 0 {
break
}
}
if dur.Nanoseconds()/ns > int64(maxInt/iter) {
return maxInt
}
return iter * int(dur.Nanoseconds()/ns)
}