Skip to content

Instantly share code, notes, and snippets.

@calmh
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calmh/c4abfb12563f9dafb9c5 to your computer and use it in GitHub Desktop.
Save calmh/c4abfb12563f9dafb9c5 to your computer and use it in GitHub Desktop.
AES256_GCM vs AES128_GCM
package crypto_test
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
)
var (
indata []byte
key []byte
)
func init() {
indata = make([]byte, 128*1024)
_, err := io.ReadFull(rand.Reader, indata)
if err != nil {
panic(err)
}
key = make([]byte, 32)
_, err = io.ReadFull(rand.Reader, indata)
if err != nil {
panic(err)
}
}
func benchmarkAES(key []byte, b *testing.B) {
bc, err := aes.NewCipher(key)
if err != nil {
b.Fatal(err)
}
sc, err := cipher.NewGCM(bc)
if err != nil {
b.Fatal(err)
}
nonce := make([]byte, sc.NonceSize())
_,err = io.ReadFull(rand.Reader, nonce)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
var out []byte
for i := 0; i < b.N; i++ {
out = out[:0]
out = sc.Seal(out, nonce, indata, nil)
}
b.SetBytes(int64(len(indata)))
}
func BenchmarkAES128(b *testing.B) {
benchmarkAES(key[:16], b)
}
func BenchmarkAES256(b *testing.B) {
benchmarkAES(key[:32], b)
}
@calmh
Copy link
Author

calmh commented Jul 14, 2014

// Darwin on 2.3GHz i7

jb@jborg-mbp:/crypto $ go version
go version devel +67f9ef140028 Sat Jul 12 15:18:36 2014 +1000 darwin/amd64
// that's Go1.3 + some
jb@jborg-mbp:
/crypto $ go test -bench . -benchtime 5s
testing: warning: no tests to run
PASS
BenchmarkAES128 5000 1199331 ns/op 109.29 MB/s
BenchmarkAES256 5000 1281047 ns/op 102.32 MB/s
ok _/Users/jb/crypto 12.685s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment