blob: 99c17eee72a88f9ea094ccd02cabb9e1115649c8 (
plain) (
blame)
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
|
package main
import (
"encoding/hex"
"fmt"
"sort"
"testing"
"time"
"roseh.moe/pkg/roseh.moe/internal/pwhash"
)
func mustHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
var salt = mustHex("34539b5dd2591415")
func benchmarkHashIter(iter int) time.Duration {
const password = "wavy upend 4z jk linda k zing four atop pi"
return time.Duration(testing.Benchmark(func(b *testing.B) {
for b.Loop() {
pwhash.HashIter(password, salt, iter)
}
}).NsPerOp())
}
func main() {
const targetTime = 5 * time.Second
iter := 8192
for ; benchmarkHashIter(iter) < targetTime; iter *= 2 {
}
lo := iter / 2
hi := iter
fmt.Println(lo + sort.Search(hi-lo, func(n int) bool {
return benchmarkHashIter(lo+n) > targetTime
}))
}
|