blob: cd144fa82c0f3ea296ef5f548a3efc8a4c594dff (
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
43
44
45
46
47
48
49
50
51
52
|
package main
import (
"encoding/hex"
"fmt"
"os"
"sort"
"testing"
"time"
"gitlab.com/rhogenson/roseh.moe/internal/cryptoutil"
)
func mustHex(s string) []byte {
bytes, err := hex.DecodeString(s)
if err != nil {
panic(fmt.Sprintf("mustHex: bad hex %q: %s", s, err))
}
return bytes
}
var (
iterations int
salt = mustHex("3fb84513fc3afcd6d3b230bf9ece91aaae2d2a99da17efbf7de83b21fafe3f08")
)
func BenchmarkHashIter(b *testing.B) {
const password = "oboe shortness ether ideology undesired fresh freezable catching mashing glimpse"
for b.Loop() {
cryptoutil.HashIter(password, salt, iterations)
}
}
func run() error {
for iterations = 8192; time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) < 500*time.Millisecond; iterations *= 2 {
}
lo := iterations / 2
hi := iterations
fmt.Println(lo + sort.Search(hi-lo, func(i int) bool {
iterations = lo + i
return time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) > 500*time.Millisecond
}))
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
|