Commit b4364e84 authored by ale's avatar ale

Add Argon2 benchmarks

parent 679daf44
// Package pwhash provides a simple interface to hashed passwords with
// support for multiple hashing algorithms.
//
// The format is the well-known dollar-separated field string,
// extended with optional algorithm-specific parameters:
//
// $id[$params...]$salt$encrypted
//
// We extend 'id' beyond the values supported by the libc crypt(3)
// function with the following hashing algorithms:
//
// Scrypt (id '$s$'), in which case the parameters are N, R and P.
//
// Argon2 (id '$a2$'), with parameters time, memory and threads. To
// tune Argon2 parameters, you can run the benchmarks in this package:
// the parameterized benchmarks are named with
// time/memory(MB)/threads. For nicer results:
//
// go test -bench=Argon2 -run=none . 2>&1 | \
// awk '/^Bench/ {ops=1000000000 / $3; print $1 " " ops " ops/sec"}'
//
package pwhash
import (
......
package pwhash
import "testing"
import (
"fmt"
"testing"
)
func TestArgon2(t *testing.T) {
testImpl(t, NewArgon2())
......@@ -57,3 +60,34 @@ func testImpl(t *testing.T, h PasswordHash) {
}
}
}
func BenchmarkArgon2(b *testing.B) {
var testParams []argon2Params
for iTime := 1; iTime <= 5; iTime++ {
for iThreads := 1; iThreads <= 8; iThreads *= 2 {
for iMem := 1; iMem <= 16; iMem *= 2 {
testParams = append(testParams, argon2Params{
Time: uint32(iTime),
Memory: uint32(iMem * 1024),
Threads: uint8(iThreads),
})
}
}
}
goodPw := "good password"
badPw := "definitely not the good password"
for _, tp := range testParams {
name := fmt.Sprintf("%d/%d/%d", tp.Time, tp.Memory, tp.Threads)
b.Run(name, func(b *testing.B) {
h := NewArgon2WithParams(tp.Time, tp.Memory, tp.Threads)
encPw := h.Encrypt(goodPw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.ComparePassword(encPw, badPw) // nolint
}
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment