1 /* 2 * Copyright (C) 2012 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package benchmarks.regression; 18 19 import com.google.caliper.BeforeExperiment; 20 import java.io.ByteArrayInputStream; 21 import java.io.InputStream; 22 import java.security.spec.AlgorithmParameterSpec; 23 import javax.crypto.Cipher; 24 import javax.crypto.CipherInputStream; 25 import javax.crypto.KeyGenerator; 26 import javax.crypto.SecretKey; 27 import javax.crypto.spec.IvParameterSpec; 28 29 /** 30 * CipherInputStream benchmark. 31 */ 32 public class CipherInputStreamBenchmark { 33 34 private static final int DATA_SIZE = 1024 * 1024; 35 private static final byte[] DATA = new byte[DATA_SIZE]; 36 37 private static final int IV_SIZE = 16; 38 private static final byte[] IV = new byte[IV_SIZE]; 39 40 static { 41 for (int i = 0; i < DATA_SIZE; i++) { 42 DATA[i] = (byte) i; 43 } 44 for (int i = 0; i < IV_SIZE; i++) { 45 IV[i] = (byte) i; 46 } 47 } 48 49 private SecretKey key; 50 51 private byte[] output = new byte[8192]; 52 53 private Cipher cipherEncrypt; 54 55 private AlgorithmParameterSpec spec; 56 57 @BeforeExperiment setUp()58 protected void setUp() throws Exception { 59 KeyGenerator generator = KeyGenerator.getInstance("AES"); 60 generator.init(128); 61 key = generator.generateKey(); 62 63 spec = new IvParameterSpec(IV); 64 65 cipherEncrypt = Cipher.getInstance("AES/CBC/PKCS5Padding"); 66 cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, spec); 67 } 68 timeEncrypt(int reps)69 public void timeEncrypt(int reps) throws Exception { 70 for (int i = 0; i < reps; ++i) { 71 cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, spec); 72 InputStream is = new CipherInputStream(new ByteArrayInputStream(DATA), cipherEncrypt); 73 while (is.read(output) != -1) { 74 } 75 } 76 } 77 } 78