You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sd...@apache.org on 2016/06/21 02:59:54 UTC

commons-crypto git commit: CRYPTO-85: CryptoCipher ENCRYPT_MODE and DECRYPT_MODE are unnecessary and confusing

Repository: commons-crypto
Updated Branches:
  refs/heads/master 0304a8707 -> 2c57b118a


CRYPTO-85: CryptoCipher ENCRYPT_MODE and DECRYPT_MODE are unnecessary and confusing


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/2c57b118
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/2c57b118
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/2c57b118

Branch: refs/heads/master
Commit: 2c57b118a30f91c182e8e6129fb25542ff2bdf43
Parents: 0304a87
Author: Sun Dapeng <sd...@apache.org>
Authored: Tue Jun 21 10:46:46 2016 +0800
Committer: Sun Dapeng <sd...@apache.org>
Committed: Tue Jun 21 10:46:46 2016 +0800

----------------------------------------------------------------------
 .../apache/commons/crypto/cipher/CryptoCipher.java | 17 ++---------------
 .../apache/commons/crypto/cipher/JceCipher.java    |  4 ++--
 .../commons/crypto/cipher/OpensslCipher.java       |  8 ++++----
 .../crypto/stream/CTRCryptoInputStream.java        |  5 +++--
 .../crypto/stream/CTRCryptoOutputStream.java       |  6 +++---
 .../commons/crypto/stream/CryptoInputStream.java   |  5 +++--
 .../commons/crypto/stream/CryptoOutputStream.java  |  6 +++---
 .../crypto/stream/PositionedCryptoInputStream.java |  5 ++---
 .../commons/crypto/cipher/AbstractCipherTest.java  |  9 +++++----
 9 files changed, 27 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
index 3b31a57..bd494fb 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
@@ -24,9 +24,7 @@ import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
 import java.util.Properties;
-
 import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 
@@ -40,18 +38,6 @@ import javax.crypto.ShortBufferException;
 public interface CryptoCipher extends Closeable {
 
     /**
-     * A constant representing encrypt mode. The mode constant to be used when
-     * calling init method of the CryptoCipher.
-     */
-    int ENCRYPT_MODE = Cipher.ENCRYPT_MODE;
-
-    /**
-     * A constant representing decrypt mode. The mode constant to be used when
-     * calling init method of the CryptoCipher.
-     */
-    int DECRYPT_MODE = Cipher.DECRYPT_MODE;
-
-    /**
      * Gets the CipherTransformation for this cipher.
      *
      * @return the CipherTransformation for this cipher.
@@ -68,7 +54,8 @@ public interface CryptoCipher extends Closeable {
     /**
      * Initializes the cipher with mode, key and iv.
      *
-     * @param mode {@link #ENCRYPT_MODE} or {@link #DECRYPT_MODE}
+     * @param mode {@link javax.crypto.Cipher#ENCRYPT_MODE} or {@link javax
+     * .crypto.Cipher#DECRYPT_MODE}
      * @param key crypto key for the cipher
      * @param params the algorithm parameters
      * @throws InvalidKeyException if the given key is inappropriate for

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java b/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
index f5bdf92..324c75f 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
@@ -84,7 +84,7 @@ public class JceCipher implements CryptoCipher {
     /**
      * Initializes the cipher with mode, key and iv.
      *
-     * @param mode {@link #ENCRYPT_MODE} or {@link #DECRYPT_MODE}
+     * @param mode {@link Cipher#ENCRYPT_MODE} or {@link Cipher#DECRYPT_MODE}
      * @param key crypto key for the cipher
      * @param params the algorithm parameters
      * @throws InvalidAlgorithmParameterException if the given algorithm
@@ -101,7 +101,7 @@ public class JceCipher implements CryptoCipher {
         Utils.checkNotNull(params);
 
         int cipherMode = Cipher.DECRYPT_MODE;
-        if (mode == ENCRYPT_MODE) {
+        if (mode == Cipher.ENCRYPT_MODE) {
             cipherMode = Cipher.ENCRYPT_MODE;
         }
         cipher.init(cipherMode, key, params);

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/cipher/OpensslCipher.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/OpensslCipher.java b/src/main/java/org/apache/commons/crypto/cipher/OpensslCipher.java
index 5594a51..37938c4 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/OpensslCipher.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/OpensslCipher.java
@@ -19,13 +19,13 @@ package org.apache.commons.crypto.cipher;
 
 import java.nio.ByteBuffer;
 import java.security.GeneralSecurityException;
-import java.security.Key;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
+import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
 import java.util.Properties;
-
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
@@ -83,7 +83,7 @@ public class OpensslCipher implements CryptoCipher {
     /**
      * Initializes the cipher with mode, key and iv.
      *
-     * @param mode {@link #ENCRYPT_MODE} or {@link #DECRYPT_MODE}
+     * @param mode {@link Cipher#ENCRYPT_MODE} or {@link Cipher#DECRYPT_MODE}
      * @param key crypto key for the cipher
      * @param params the algorithm parameters
      * @throws InvalidKeyException If key length is invalid
@@ -96,7 +96,7 @@ public class OpensslCipher implements CryptoCipher {
         Utils.checkNotNull(params);
 
         int cipherMode = Openssl.DECRYPT_MODE;
-        if (mode == ENCRYPT_MODE) {
+        if (mode == Cipher.ENCRYPT_MODE) {
             cipherMode = Openssl.ENCRYPT_MODE;
         }
         byte[] iv;

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java
index 50de2f6..33a032d 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java
@@ -25,13 +25,14 @@ import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.util.Properties;
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
-import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.cipher.CipherTransformation;
+import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.stream.input.ChannelInput;
 import org.apache.commons.crypto.stream.input.Input;
 import org.apache.commons.crypto.stream.input.StreamInput;
@@ -568,7 +569,7 @@ public class CTRCryptoInputStream extends CryptoInputStream {
         final long counter = getCounter(position);
         Utils.calculateIV(initIV, counter, iv);
         try {
-            cipher.init(CryptoCipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
+            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
         } catch (InvalidKeyException e) {
             throw new IOException(e);
         } catch (InvalidAlgorithmParameterException e) {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java
index 4a25c95..7d13a39 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java
@@ -24,15 +24,15 @@ import java.nio.channels.WritableByteChannel;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.util.Properties;
-
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
-import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.cipher.CipherTransformation;
+import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.stream.output.ChannelOutput;
 import org.apache.commons.crypto.stream.output.Output;
 import org.apache.commons.crypto.stream.output.StreamOutput;
@@ -329,7 +329,7 @@ public class CTRCryptoOutputStream extends CryptoOutputStream {
 
         Utils.calculateIV(initIV, counter, iv);
         try {
-            cipher.init(CryptoCipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
+            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
         } catch (InvalidKeyException e) {
             throw new IOException(e);
         } catch (InvalidAlgorithmParameterException e) {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
index db974ab..faeda11 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
@@ -27,12 +27,13 @@ import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
 import java.util.Properties;
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
 
-import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.cipher.CipherTransformation;
+import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.stream.input.ChannelInput;
 import org.apache.commons.crypto.stream.input.Input;
 import org.apache.commons.crypto.stream.input.StreamInput;
@@ -453,7 +454,7 @@ public class CryptoInputStream extends InputStream implements
      */
     protected void initCipher() throws IOException {
         try {
-            cipher.init(CryptoCipher.DECRYPT_MODE, key, params);
+            cipher.init(Cipher.DECRYPT_MODE, key, params);
         } catch (InvalidKeyException e) {
             throw new IOException(e);
         } catch (InvalidAlgorithmParameterException e) {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
index 01bd691..b14d7fe 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
@@ -27,14 +27,14 @@ import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
 import java.util.Properties;
-
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
 
-import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.cipher.CipherTransformation;
+import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.stream.output.ChannelOutput;
 import org.apache.commons.crypto.stream.output.Output;
 import org.apache.commons.crypto.stream.output.StreamOutput;
@@ -326,7 +326,7 @@ public class CryptoOutputStream extends OutputStream implements
      */
     protected void initCipher() throws IOException {
         try {
-            cipher.init(CryptoCipher.ENCRYPT_MODE, key, params);
+            cipher.init(Cipher.ENCRYPT_MODE, key, params);
         } catch (InvalidKeyException e) {
             throw new IOException(e);
         } catch (InvalidAlgorithmParameterException e) {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
index 3f0ee76..cbd77fb 100644
--- a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
@@ -25,8 +25,8 @@ import java.security.InvalidKeyException;
 import java.util.Properties;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
-
 import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
@@ -36,7 +36,6 @@ import org.apache.commons.crypto.cipher.CryptoCipherFactory;
 import org.apache.commons.crypto.stream.input.Input;
 import org.apache.commons.crypto.utils.IOUtils;
 import org.apache.commons.crypto.utils.Utils;
-
 import static org.apache.commons.crypto.cipher.CipherTransformation.AES_CTR_NOPADDING;
 
 /**
@@ -293,7 +292,7 @@ public class PositionedCryptoInputStream extends CTRCryptoInputStream {
         final long counter = getCounter(position);
         Utils.calculateIV(getInitIV(), counter, iv);
         try {
-            state.getCipher().init(CryptoCipher.DECRYPT_MODE, key,
+            state.getCipher().init(Cipher.DECRYPT_MODE, key,
                     new IvParameterSpec(iv));
         } catch (InvalidKeyException e) {
             throw new IOException(e);

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/2c57b118/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
index 00d1505..857d681 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -23,6 +23,7 @@ import java.security.GeneralSecurityException;
 import java.security.SecureRandom;
 import java.util.Properties;
 import java.util.Random;
+import javax.crypto.Cipher;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 import javax.xml.bind.DatatypeConverter;
@@ -107,14 +108,14 @@ public abstract class AbstractCipherTest {
         dec = getCipher(transformation);
 
         try {
-            enc.init(CryptoCipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),
+            enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),
                     new IvParameterSpec(iv));
         } catch (Exception e) {
             Assert.fail("AES failed initialisation - " + e.toString());
         }
 
         try {
-            dec.init(CryptoCipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
+            dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
                     new IvParameterSpec(iv));
         } catch (Exception e) {
             Assert.fail("AES failed initialisation - " + e.toString());
@@ -237,14 +238,14 @@ public abstract class AbstractCipherTest {
         dec = getCipher(transformation);
 
         try {
-            enc.init(CryptoCipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),
+            enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),
                     new IvParameterSpec(iv));
         } catch (Exception e) {
             Assert.fail("AES failed initialisation - " + e.toString());
         }
 
         try {
-            dec.init(CryptoCipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
+            dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
                     new IvParameterSpec(iv));
         } catch (Exception e) {
             Assert.fail("AES failed initialisation - " + e.toString());