Skip to content

Instantly share code, notes, and snippets.

@mmichaelb
Last active April 29, 2018 18:53
Show Gist options
  • Save mmichaelb/99dc21754bb836981c9252ff671024eb to your computer and use it in GitHub Desktop.
Save mmichaelb/99dc21754bb836981c9252ff671024eb to your computer and use it in GitHub Desktop.
Golang Argon2id Algorithms Benchmark
package main
import (
"bytes"
tvdburgtArgon2 "github.com/tvdburgt/go-argon2"
goCryptoArgon2 "golang.org/x/crypto/argon2"
"log"
"strconv"
"testing"
)
var (
password = []byte("ThisIsABenchmark!123")
salt = []byte("12345678")
iterations = 4
memory = 64 * 1024
threads = 1
keyLen = 32
expectedHash = []byte{99, 252, 69, 208, 213, 226, 127, 170, 187, 85, 201, 231, 149, 49, 30, 147, 104, 77, 42, 178, 69, 87, 53, 168, 151, 83, 65, 125, 14, 21, 197, 133}
)
func BenchmarkGolangCryptoArgon2(b *testing.B) {
benchmarkAlgorithm(b, "golang.org/x/crypto/argon2", password, salt, iterations, memory, threads, keyLen, benchmarkGolangCryptoArgon2)
}
func BenchmarkTvdburgtArgon2(b *testing.B) {
benchmarkAlgorithm(b, "github.com/tvdburgt/go-argon2", password, salt, iterations, memory, threads, keyLen, benchmarkTvdburgtArgon2)
}
func benchmarkAlgorithm(b *testing.B, name string, password, salt []byte, iterations, memory, threads, keyLen int,
testingInitFunc func(password, salt []byte, time, memory, threads, keyLen int) func() ([]byte, error)) {
hashGenerateFunc := testingInitFunc(password, salt, iterations, memory, threads, keyLen)
var err error
var hash []byte
b.ResetTimer()
for n := 0; n < b.N; n++ {
hash, err = hashGenerateFunc()
if err != nil {
log.Printf("An error occurred while testing: %v\n", strconv.Quote(name))
}
}
b.StopTimer()
if !bytes.Equal(hash, expectedHash) {
b.Fail()
}
}
func benchmarkGolangCryptoArgon2(password, salt []byte, time, memory, threads, keyLen int) func() (hash []byte, err error) {
return func() (hash []byte, err error) {
hash = goCryptoArgon2.IDKey(password, salt, uint32(time), uint32(memory), uint8(threads), uint32(keyLen))
return
}
}
func benchmarkTvdburgtArgon2(password, salt []byte, time, memory, threads, keyLen int) func() (hash []byte, err error) {
defaultArgon2Context := &tvdburgtArgon2.Context{
Iterations: time,
Memory: memory,
Parallelism: threads,
HashLen: keyLen,
Mode: tvdburgtArgon2.ModeArgon2id,
Version: tvdburgtArgon2.Version13,
}
return func() (hash []byte, err error) {
return tvdburgtArgon2.Hash(defaultArgon2Context, []byte(password), salt)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment