Skip to content

Instantly share code, notes, and snippets.

@hamakn
Created February 2, 2018 08:50
Show Gist options
  • Save hamakn/6418fa89c798280ef9af42a179ec9bc7 to your computer and use it in GitHub Desktop.
Save hamakn/6418fa89c798280ef9af42a179ec9bc7 to your computer and use it in GitHub Desktop.
benchmark for bcrypt and scrypt
package crypto
import (
"testing"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/scrypt"
)
func BenchmarkBcryptHashCost10(b *testing.B) {
bstr := []byte("foobar")
b.ResetTimer()
for i := 0; i < b.N; i++ {
bcrypt.GenerateFromPassword(bstr, bcrypt.DefaultCost)
}
}
func BenchmarkBcryptHashCost4(b *testing.B) {
bstr := []byte("foobar")
b.ResetTimer()
for i := 0; i < b.N; i++ {
bcrypt.GenerateFromPassword(bstr, bcrypt.MinCost)
}
}
func BenchmarkScryptHashN32768(b *testing.B) {
bstr := []byte("foobar")
bsalt := []byte("salt")
b.ResetTimer()
for i := 0; i < b.N; i++ {
// https://github.com/golang/crypto/blob/3d37316aaa6bd9929127ac9a527abf408178ea7b/scrypt/scrypt.go#L223-L224
// The recommended parameters for interactive logins as of 2017 are N=32768, r=8 and p=1.
scrypt.Key(bstr, bsalt, 32768, 8, 1, 32)
}
}
func BenchmarkScryptHashN256(b *testing.B) {
bstr := []byte("foobar")
bsalt := []byte("salt")
b.ResetTimer()
for i := 0; i < b.N; i++ {
scrypt.Key(bstr, bsalt, 256, 8, 1, 32)
}
}
// =>
// BenchmarkBCryptHashCost10-4 20 92228057 ns/op 5204 B/op 11 allocs/op
// BenchmarkBCryptHashCost4-4 1000 1272765 ns/op 5169 B/op 11 allocs/op
// BenchmarkSCryptHashN32768-4 10 129435336 ns/op 33558579 B/op 18 allocs/op
// BenchmarkSCryptHashN256-4 2000 878410 ns/op 266280 B/op 18 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment