Repository: commons-crypto
Updated Branches:
  refs/heads/master 081284dce -> c0df81587


CRYPTO-3: Change default cipher as OpensslCipher

Addressing comments

Handle the edge case at getCipherClassString and handle the null property for 
getInstance


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

Branch: refs/heads/master
Commit: c0df8158789327a4737b58769da2f321447ae18c
Parents: 081284d
Author: Ferdinand Xu <cheng.a...@intel.com>
Authored: Fri Apr 15 07:15:30 2016 +0800
Committer: Ferdinand Xu <cheng.a...@intel.com>
Committed: Tue Apr 26 05:50:54 2016 +0800

----------------------------------------------------------------------
 .../commons/crypto/cipher/CipherFactory.java    |  8 ++-
 .../commons/crypto/conf/ConfigurationKeys.java  |  4 +-
 .../org/apache/commons/crypto/utils/Utils.java  |  7 ++-
 .../crypto/cipher/CipherFactoryTest.java        | 57 ++++++++++++++++++++
 4 files changed, 68 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c0df8158/src/main/java/org/apache/commons/crypto/cipher/CipherFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/CipherFactory.java 
b/src/main/java/org/apache/commons/crypto/cipher/CipherFactory.java
index fb712d2..fdaf64c 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/CipherFactory.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/CipherFactory.java
@@ -79,17 +79,15 @@ public class CipherFactory {
    */
   public static Cipher getInstance(CipherTransformation transformation)
       throws GeneralSecurityException {
-    return getInstance(transformation, null);
+    return getInstance(transformation, new Properties());
   }
 
+  // Return OpenSSLCipher if Properties is null or empty by default
   private static List<Class<? extends Cipher>> getCipherClasses(Properties 
props) {
     List<Class<? extends Cipher>> result = new ArrayList<Class<? extends
         Cipher>>();
     String cipherClassString = Utils.getCipherClassString(props);
-    if (cipherClassString == null) {
-      LOG.debug("No cipher classes configured.");
-      return null;
-    }
+
     for (String c : Utils.splitClassNames(cipherClassString, ",")) {
       try {
         Class<?> cls = ReflectionUtils.getClassByName(c);

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c0df8158/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java 
b/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
index 494c28d..ae07758 100644
--- a/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
+++ b/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
@@ -17,7 +17,7 @@
  */
 package org.apache.commons.crypto.conf;
 
-import org.apache.commons.crypto.cipher.JceCipher;
+import org.apache.commons.crypto.cipher.OpensslCipher;
 
 /**
  * The ConfigurationKeys contains Configuration keys and default values.
@@ -53,7 +53,7 @@ public class ConfigurationKeys {
    * The default value for crypto cipher.
    */
   public static final String COMMONS_CRYPTO_CIPHER_CLASSES_DEFAULT =
-      JceCipher.class.getName();
+      OpensslCipher.class.getName();
 
   /**
    * The configuration key of the provider class for JCE cipher.

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c0df8158/src/main/java/org/apache/commons/crypto/utils/Utils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java 
b/src/main/java/org/apache/commons/crypto/utils/Utils.java
index ec2a1e4..3c2ef86 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -138,8 +138,13 @@ public class Utils {
    */
   public static String getCipherClassString(Properties props) {
     final String configName = COMMONS_CRYPTO_CIPHER_CLASSES_KEY;
-    return props.getProperty(configName) != null ? 
props.getProperty(configName) : System
+    String cipherClassString = props.getProperty(configName) != null ? props
+        .getProperty(configName, COMMONS_CRYPTO_CIPHER_CLASSES_DEFAULT) : 
System
         .getProperty(configName, COMMONS_CRYPTO_CIPHER_CLASSES_DEFAULT);
+    if (cipherClassString.isEmpty()) {
+      cipherClassString = COMMONS_CRYPTO_CIPHER_CLASSES_DEFAULT;
+    }
+    return cipherClassString;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c0df8158/src/test/java/org/apache/commons/crypto/cipher/CipherFactoryTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/crypto/cipher/CipherFactoryTest.java 
b/src/test/java/org/apache/commons/crypto/cipher/CipherFactoryTest.java
new file mode 100644
index 0000000..cd995df
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/cipher/CipherFactoryTest.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.crypto.cipher;
+
+import java.security.GeneralSecurityException;
+import java.util.Properties;
+
+import org.apache.commons.crypto.conf.ConfigurationKeys;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class CipherFactoryTest {
+  @Test
+  public void testDefaultCipher() throws GeneralSecurityException {
+    Cipher defaultCipher = CipherFactory.getInstance(
+        CipherTransformation.AES_CBC_NOPADDING);
+    Assert.assertEquals(OpensslCipher.class.getName(),
+        defaultCipher.getClass().getName());
+  }
+
+  @Test
+  public void testEmptyCipher() throws GeneralSecurityException {
+    Properties properties = new Properties();
+    properties.put(ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY, "");
+    Cipher defaultCipher = CipherFactory.getInstance(
+        CipherTransformation.AES_CBC_NOPADDING, properties);
+    Assert.assertEquals(OpensslCipher.class.getName(),
+        defaultCipher.getClass().getName());
+  }
+
+  @Test
+  public void testInvalidCipher() throws GeneralSecurityException {
+    Properties properties = new Properties();
+    properties.put(ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY,
+        "InvalidCipherName");
+    Cipher defaultCipher = CipherFactory.getInstance(
+        CipherTransformation.AES_CBC_NOPADDING, properties);
+    Assert.assertEquals(JceCipher.class.getName(),
+        defaultCipher.getClass().getName());
+  }
+}

Reply via email to