From bedcfb11c3c4079387e8c463cf256c473007771a Mon Sep 17 00:00:00 2001
From: Christian Wulf <chw@informatik.uni-kiel.de>
Date: Wed, 22 Apr 2015 08:53:00 +0200
Subject: [PATCH] refactored CipherStage

---
 .settings/edu.umd.cs.findbugs.core.prefs      |  2 +-
 ...{CipherByteArray.java => CipherStage.java} | 36 +++++++++----------
 .../examples/cipher/CipherConfiguration.java  |  8 ++---
 .../tokenizer/TokenizerConfiguration.java     |  6 ++--
 ...yteArrayTest.java => CipherStageTest.java} |  8 ++---
 5 files changed, 29 insertions(+), 31 deletions(-)
 rename src/main/java/teetime/stage/{CipherByteArray.java => CipherStage.java} (71%)
 rename src/test/java/teetime/stage/{CipherByteArrayTest.java => CipherStageTest.java} (85%)

diff --git a/.settings/edu.umd.cs.findbugs.core.prefs b/.settings/edu.umd.cs.findbugs.core.prefs
index d18c2f7f..f67d8103 100644
--- a/.settings/edu.umd.cs.findbugs.core.prefs
+++ b/.settings/edu.umd.cs.findbugs.core.prefs
@@ -1,5 +1,5 @@
 #FindBugs User Preferences
-#Tue Apr 21 16:50:39 CEST 2015
+#Wed Apr 22 08:29:19 CEST 2015
 detector_threshold=3
 effort=max
 excludefilter0=.fbExcludeFilterFile|true
diff --git a/src/main/java/teetime/stage/CipherByteArray.java b/src/main/java/teetime/stage/CipherStage.java
similarity index 71%
rename from src/main/java/teetime/stage/CipherByteArray.java
rename to src/main/java/teetime/stage/CipherStage.java
index 6fbacf19..78d2ef2a 100644
--- a/src/main/java/teetime/stage/CipherByteArray.java
+++ b/src/main/java/teetime/stage/CipherStage.java
@@ -20,7 +20,9 @@ import java.security.NoSuchAlgorithmException;
 import java.security.spec.InvalidKeySpecException;
 import java.security.spec.KeySpec;
 
+import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
 import javax.crypto.SecretKeyFactory;
@@ -30,19 +32,20 @@ import javax.crypto.spec.SecretKeySpec;
 import teetime.framework.AbstractConsumerStage;
 import teetime.framework.OutputPort;
 
-public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
+public final class CipherStage extends AbstractConsumerStage<byte[]> {
 
 	private final OutputPort<byte[]> outputPort = this.createOutputPort();
-	private Cipher cipher = null;
+	private Cipher cipher;
 
 	public enum CipherMode {
 		ENCRYPT, DECRYPT
 	}
 
-	public CipherByteArray(final String password, final CipherMode mode) {
-		final byte[] salt = { 't', 'e', 's', 't' };
-		SecretKeySpec skeyspec = null;
+	public CipherStage(final String password, final CipherMode mode) {
+		this(password, mode, new byte[] { 't', 'e', 's', 't' });
+	}
 
+	public CipherStage(final String password, final CipherMode mode, final byte[] salt) {
 		final KeySpec keySpec = new PBEKeySpec(password.toCharArray(),
 				salt,
 				1024, 128);
@@ -58,7 +61,7 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
 			throw new IllegalStateException(e1);
 		}
 
-		skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
+		final SecretKeySpec skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
 
 		try {
 			this.cipher = Cipher.getInstance(skeyspec.getAlgorithm());
@@ -68,12 +71,9 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
 			throw new IllegalStateException(e);
 		}
 
+		final int convertedMode = (mode == CipherMode.ENCRYPT) ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
 		try {
-			if (mode == CipherMode.ENCRYPT) {
-				this.cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
-			} else {
-				this.cipher.init(Cipher.DECRYPT_MODE, skeyspec);
-			}
+			this.cipher.init(convertedMode, skeyspec);
 		} catch (final InvalidKeyException e) {
 			throw new IllegalStateException(e);
 		}
@@ -81,16 +81,14 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
 
 	@Override
 	protected void execute(final byte[] element) {
-
-		byte[] output = null;
-
 		try {
-			output = this.cipher.doFinal(element);
-		} catch (final Exception e) {
-			e.printStackTrace();
+			byte[] output = this.cipher.doFinal(element);
+			this.outputPort.send(output);
+		} catch (IllegalBlockSizeException e) {
+			throw new IllegalStateException(e);
+		} catch (BadPaddingException e) {
+			throw new IllegalStateException(e);
 		}
-
-		this.outputPort.send(output);
 	}
 
 	public OutputPort<byte[]> getOutputPort() {
diff --git a/src/test/java/teetime/examples/cipher/CipherConfiguration.java b/src/test/java/teetime/examples/cipher/CipherConfiguration.java
index 64dbbb0b..fca97df2 100644
--- a/src/test/java/teetime/examples/cipher/CipherConfiguration.java
+++ b/src/test/java/teetime/examples/cipher/CipherConfiguration.java
@@ -18,8 +18,8 @@ package teetime.examples.cipher;
 import java.io.File;
 
 import teetime.framework.AnalysisConfiguration;
-import teetime.stage.CipherByteArray;
-import teetime.stage.CipherByteArray.CipherMode;
+import teetime.stage.CipherStage;
+import teetime.stage.CipherStage.CipherMode;
 import teetime.stage.InitialElementProducer;
 import teetime.stage.ZipByteArray;
 import teetime.stage.ZipByteArray.ZipMode;
@@ -34,10 +34,10 @@ public class CipherConfiguration extends AnalysisConfiguration {
 
 		final InitialElementProducer<File> init = new InitialElementProducer<File>(input);
 		final File2ByteArray f2b = new File2ByteArray();
-		final CipherByteArray enc = new CipherByteArray(password, CipherMode.ENCRYPT);
+		final CipherStage enc = new CipherStage(password, CipherMode.ENCRYPT);
 		final ZipByteArray comp = new ZipByteArray(ZipMode.COMP);
 		final ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
-		final CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
+		final CipherStage decrypt = new CipherStage(password, CipherMode.DECRYPT);
 		final ByteArrayFileWriter writer = new ByteArrayFileWriter(output);
 
 		connectIntraThreads(init.getOutputPort(), f2b.getInputPort());
diff --git a/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java b/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java
index 129cdd4f..e6b0f9d7 100644
--- a/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java
+++ b/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java
@@ -19,8 +19,8 @@ import java.io.File;
 
 import teetime.framework.AnalysisConfiguration;
 import teetime.stage.ByteArray2String;
-import teetime.stage.CipherByteArray;
-import teetime.stage.CipherByteArray.CipherMode;
+import teetime.stage.CipherStage;
+import teetime.stage.CipherStage.CipherMode;
 import teetime.stage.Counter;
 import teetime.stage.InitialElementProducer;
 import teetime.stage.ZipByteArray;
@@ -38,7 +38,7 @@ public class TokenizerConfiguration extends AnalysisConfiguration {
 		final InitialElementProducer<File> init = new InitialElementProducer<File>(input);
 		final File2ByteArray f2b = new File2ByteArray();
 		final ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
-		final CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
+		final CipherStage decrypt = new CipherStage(password, CipherMode.DECRYPT);
 		final ByteArray2String b2s = new ByteArray2String();
 		final Tokenizer tokenizer = new Tokenizer(" ");
 		this.counter = new Counter<String>();
diff --git a/src/test/java/teetime/stage/CipherByteArrayTest.java b/src/test/java/teetime/stage/CipherStageTest.java
similarity index 85%
rename from src/test/java/teetime/stage/CipherByteArrayTest.java
rename to src/test/java/teetime/stage/CipherStageTest.java
index ee1dcd68..4f2d8799 100644
--- a/src/test/java/teetime/stage/CipherByteArrayTest.java
+++ b/src/test/java/teetime/stage/CipherStageTest.java
@@ -24,17 +24,17 @@ import java.util.List;
 
 import org.junit.Test;
 
-import teetime.stage.CipherByteArray.CipherMode;
+import teetime.stage.CipherStage.CipherMode;
 
 /**
  * @author Nils Christian Ehmke
  */
-public class CipherByteArrayTest {
+public class CipherStageTest {
 
 	@Test
 	public void decryptShouldInvertEncryption() {
-		final CipherByteArray encryptStage = new CipherByteArray("somePassword", CipherMode.ENCRYPT);
-		final CipherByteArray decryptStage = new CipherByteArray("somePassword", CipherMode.DECRYPT);
+		final CipherStage encryptStage = new CipherStage("somePassword", CipherMode.ENCRYPT);
+		final CipherStage decryptStage = new CipherStage("somePassword", CipherMode.DECRYPT);
 
 		final byte[] input = new byte[] { 1, 2, 3, 4, 5 };
 		final List<byte[]> encryptedResult = new ArrayList<byte[]>();
-- 
GitLab