Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "fmt"
- "io"
- "testing"
- "github.com/alecthomas/units"
- )
- func newCipher(keySize int64) cipher.AEAD {
- key := make([]byte, keySize)
- io.ReadFull(rand.Reader, key)
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- aead, err := cipher.NewGCM(block)
- if err != nil {
- panic(err)
- }
- return aead
- }
- func benchmarkSeal(b *testing.B, chunkSize units.Base2Bytes, aead cipher.AEAD) {
- plaintext := make([]byte, chunkSize)
- nonce := make([]byte, 12)
- b.SetBytes(int64(chunkSize))
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- aead.Seal(nil, nonce, plaintext, nil)
- }
- }
- func benchmarkOpen(b *testing.B, chunkSize units.Base2Bytes, aead cipher.AEAD) {
- nonce := make([]byte, 12)
- ciphertext := aead.Seal(nil, nonce, make([]byte, chunkSize), nil)
- b.SetBytes(int64(chunkSize))
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- aead.Open(nil, nonce, ciphertext, nil)
- }
- }
- func BenchmarkGCM(b *testing.B) {
- for _, keySize := range []int64{16, 32} {
- for _, chunkSize := range []units.Base2Bytes{units.KiB, units.MiB, units.GiB} {
- aead := newCipher(keySize)
- seal := fmt.Sprintf("AES-%d-GCM-Seal-%s", int64(keySize)*8, chunkSize.String())
- open := fmt.Sprintf("AES-%d-GCM-Open-%s", int64(keySize)*8, chunkSize.String())
- b.Run(seal, func(b *testing.B) { benchmarkSeal(b, chunkSize, aead) })
- b.Run(open, func(b *testing.B) { benchmarkOpen(b, chunkSize, aead) })
- }
- }
- }
Add Comment
Please, Sign In to add comment