Last active
August 29, 2015 14:03
-
-
Save calmh/c4abfb12563f9dafb9c5 to your computer and use it in GitHub Desktop.
AES256_GCM vs AES128_GCM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Darwin on 2.3GHz i7
jb@jborg-mbp:
/crypto $ go version/crypto $ go test -bench . -benchtime 5sgo version devel +67f9ef140028 Sat Jul 12 15:18:36 2014 +1000 darwin/amd64
// that's Go1.3 + some
jb@jborg-mbp:
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