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/20 02:27:00 UTC
commons-crypto git commit: CRYPTO-76: Remove log dependence
Repository: commons-crypto
Updated Branches:
refs/heads/master bccccdbfd -> 4117c5696
CRYPTO-76: Remove log dependence
Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/4117c569
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/4117c569
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/4117c569
Branch: refs/heads/master
Commit: 4117c569644cce4684528c35fdfcc2947233a680
Parents: bccccdb
Author: Sun Dapeng <sd...@apache.org>
Authored: Mon Jun 20 10:03:18 2016 +0800
Committer: Sun Dapeng <sd...@apache.org>
Committed: Mon Jun 20 10:21:16 2016 +0800
----------------------------------------------------------------------
pom.xml | 2 +
.../crypto/cipher/CryptoCipherFactory.java | 62 ++++++--------------
.../apache/commons/crypto/cipher/Openssl.java | 6 --
.../commons/crypto/conf/ConfigurationKeys.java | 19 +++++-
.../crypto/random/CryptoRandomFactory.java | 23 ++++----
.../crypto/random/OpensslCryptoRandom.java | 7 +--
.../commons/crypto/random/OsCryptoRandom.java | 7 +--
.../apache/commons/crypto/utils/IOUtils.java | 8 +--
.../commons/crypto/utils/NativeCodeLoader.java | 24 ++------
.../org/apache/commons/crypto/utils/Utils.java | 23 +++++++-
.../crypto/cipher/CryptoCipherFactoryTest.java | 23 +++++++-
11 files changed, 102 insertions(+), 102 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index b12e573..4529a5f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -498,11 +498,13 @@ The following provides more details on the included cryptographic software:
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
index 9e59cff..3509162 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java
@@ -18,24 +18,17 @@
package org.apache.commons.crypto.cipher;
import java.security.GeneralSecurityException;
-import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.crypto.utils.ReflectionUtils;
import org.apache.commons.crypto.utils.Utils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* This is the factory class used for creating cipher class
*/
public class CryptoCipherFactory {
- /** LOG instance for {@link CryptoCipherFactory} */
- private final static Logger LOG = LoggerFactory
- .getLogger(CryptoCipherFactory.class);
-
/**
* The private Constructor of {@link CryptoCipherFactory}.
*/
@@ -53,28 +46,36 @@ public class CryptoCipherFactory {
*/
public static CryptoCipher getInstance(CipherTransformation transformation,
Properties props) throws GeneralSecurityException {
- List<Class<? extends CryptoCipher>> klasses = getCipherClasses(props);
+
+ List<String> klasses = Utils.splitClassNames(
+ Utils.getCipherClassString(props), ",");
CryptoCipher cipher = null;
+
+ StringBuilder errorMessage = new StringBuilder("CryptoCipher ");
if (klasses != null) {
- for (Class<? extends CryptoCipher> klass : klasses) {
+ for (String klass : klasses) {
try {
- cipher = ReflectionUtils.newInstance(klass, props,
- transformation);
+ Class<?> cls = ReflectionUtils.getClassByName(klass);
+ cipher = ReflectionUtils.newInstance(cls.asSubclass
+ (CryptoCipher.class), props, transformation);
if (cipher != null) {
- LOG.debug("Using cipher {} for transformation {}.",
- klass.getName(), transformation.getName());
break;
}
} catch (Exception e) {
- LOG.error(
- "CryptoCipher {} is not available or transformation {} is not "
- + "supported.", klass.getName(),
- transformation.getName());
+ errorMessage.append("{" + klass + "}");
}
}
}
- return (cipher == null) ? new JceCipher(props, transformation) : cipher;
+ if (cipher != null) {
+ return cipher;
+ } else if (Utils.isFallbackEnable(props)) {
+ return new JceCipher(props,transformation);
+ } else {
+ errorMessage.append(" is not available or transformation " +
+ transformation.getName() + " is not supported.");
+ throw new GeneralSecurityException(errorMessage.toString());
+ }
}
/**
@@ -91,29 +92,4 @@ public class CryptoCipherFactory {
return getInstance(transformation, new Properties());
}
- /**
- * Returns OpenSSLCipher if Properties is null or empty by default.
- *
- * @param props the configuration properties.
- * @return the OpenSSLCipher instance.
- */
- private static List<Class<? extends CryptoCipher>> getCipherClasses(
- Properties props) {
- List<Class<? extends CryptoCipher>> result = new ArrayList<Class<? extends CryptoCipher>>();
- String cipherClassString = Utils.getCipherClassString(props);
-
- for (String c : Utils.splitClassNames(cipherClassString, ",")) {
- try {
- Class<?> cls = ReflectionUtils.getClassByName(c);
- result.add(cls.asSubclass(CryptoCipher.class));
- } catch (ClassCastException e) {
- LOG.error("Class {} is not a CryptoCipher.", c);
- } catch (ClassNotFoundException e) {
- LOG.error("CryptoCipher {} not found.", c);
- }
- }
-
- return result;
- }
-
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/Openssl.java b/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
index 34989ed..0953202 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/Openssl.java
@@ -20,15 +20,11 @@ package org.apache.commons.crypto.cipher;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.StringTokenizer;
-
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
import org.apache.commons.crypto.utils.NativeCodeLoader;
import org.apache.commons.crypto.utils.Utils;
@@ -37,7 +33,6 @@ import org.apache.commons.crypto.utils.Utils;
* It's flexible to add other crypto algorithms/modes.
*/
final class Openssl {
- private static final Log LOG = LogFactory.getLog(Openssl.class.getName());
// Mode constant defined by Openssl JNI
public static final int ENCRYPT_MODE = 1;
@@ -101,7 +96,6 @@ final class Openssl {
}
} catch (Throwable t) {
loadingFailure = t.getMessage();
- LOG.debug("Failed to load OpenSSL CryptoCipher.", t);
} finally {
loadingFailureReason = loadingFailure;
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/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 04d89b2..d808f0d 100644
--- a/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
+++ b/src/main/java/org/apache/commons/crypto/conf/ConfigurationKeys.java
@@ -130,9 +130,22 @@ public class ConfigurationKeys {
public static final String COMMONS_CRYPTO_LIB_TEMPDIR_KEY = CONF_PREFIX
+ "lib.tempdir";
- /**
- * The private constructor of {@Link ConfigurationKeys}.
- */
+ /**
+ * The configuration key of enable fallback on native failed.
+ */
+ public static final String
+ COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY = CONF_PREFIX +
+ "enable.fallback";
+
+ /**
+ * The default value of enable fallback on native failed.
+ */
+ public static final boolean
+ COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_DEFAULT = true;
+
+ /**
+ * The private constructor of {@Link ConfigurationKeys}.
+ */
private ConfigurationKeys() {
}
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
index f1dc270..a97a853 100644
--- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
+++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java
@@ -20,20 +20,14 @@ package org.apache.commons.crypto.random;
import java.security.GeneralSecurityException;
import java.util.Properties;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.apache.commons.crypto.utils.Utils;
import org.apache.commons.crypto.utils.ReflectionUtils;
-
+import org.apache.commons.crypto.utils.Utils;
import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_SECURE_RANDOM_CLASSES_KEY;
/**
* This is the factory class used for {@link CryptoRandom}.
*/
public class CryptoRandomFactory {
- public final static Logger LOG = LoggerFactory
- .getLogger(CryptoRandomFactory.class);
/**
* The private constructor of {@Link CryptoRandomFactory}.
@@ -64,6 +58,7 @@ public class CryptoRandomFactory {
.getProperty(COMMONS_CRYPTO_SECURE_RANDOM_CLASSES_KEY);
}
+ StringBuilder errorMessage = new StringBuilder();
CryptoRandom random = null;
if (cryptoRandomClasses != null) {
for (String klassName : Utils.splitClassNames(cryptoRandomClasses,
@@ -77,13 +72,21 @@ public class CryptoRandomFactory {
break;
}
} catch (ClassCastException e) {
- LOG.error("Class {} is not a CryptoCipher.", klassName);
+ errorMessage.append("Class: [" + klassName + "] is not a " +
+ "CryptoCipher.");
} catch (ClassNotFoundException e) {
- LOG.error("CryptoCipher {} not found.", klassName);
+ errorMessage.append("CryptoCipher: [" + klassName + "] " +
+ "not " + "found.");
}
}
}
- return (random == null) ? new JavaCryptoRandom(props) : random;
+ if (random != null) {
+ return random;
+ } else if (Utils.isFallbackEnable(props)) {
+ return new JavaCryptoRandom(props);
+ } else {
+ throw new GeneralSecurityException(errorMessage.toString());
+ }
}
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java b/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
index 1813624..af78ddd 100644
--- a/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/random/OpensslCryptoRandom.java
@@ -21,9 +21,6 @@ import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.Random;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
import org.apache.commons.crypto.utils.NativeCodeLoader;
import org.apache.commons.crypto.utils.Utils;
@@ -46,8 +43,6 @@ import org.apache.commons.crypto.utils.Utils;
*/
public class OpensslCryptoRandom extends Random implements CryptoRandom {
private static final long serialVersionUID = -7828193502768789584L;
- private static final Log LOG = LogFactory.getLog(OpensslCryptoRandom.class
- .getName());
/** If native SecureRandom unavailable, use java SecureRandom */
private final JavaCryptoRandom fallback;
@@ -60,7 +55,7 @@ public class OpensslCryptoRandom extends Random implements CryptoRandom {
OpensslCryptoRandomNative.initSR();
opensslLoaded = true;
} catch (Throwable t) {
- LOG.error("Failed to load Openssl CryptoRandom", t);
+ ; // NOPMD
}
}
nativeEnabled = opensslLoaded;
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/random/OsCryptoRandom.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/random/OsCryptoRandom.java b/src/main/java/org/apache/commons/crypto/random/OsCryptoRandom.java
index d53f074..1525023 100644
--- a/src/main/java/org/apache/commons/crypto/random/OsCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/random/OsCryptoRandom.java
@@ -25,19 +25,16 @@ import java.util.Random;
import org.apache.commons.crypto.utils.IOUtils;
import org.apache.commons.crypto.utils.Utils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
/**
* A Random implementation that uses random bytes sourced from the operating
* system.
*/
public class OsCryptoRandom extends Random implements CryptoRandom {
- public static final Log LOG = LogFactory.getLog(OsCryptoRandom.class);
private static final long serialVersionUID = 6391500337172057900L;
- private final int RESERVOIR_LENGTH = 8192;
+ private static final int RESERVOIR_LENGTH = 8192;
private String randomDevPath;
@@ -131,7 +128,7 @@ public class OsCryptoRandom extends Random implements CryptoRandom {
@Override
synchronized public void close() {
if (stream != null) {
- IOUtils.cleanup(LOG, stream);
+ IOUtils.cleanup(stream);
stream = null;
}
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/utils/IOUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/IOUtils.java b/src/main/java/org/apache/commons/crypto/utils/IOUtils.java
index a766613..521bdf2 100644
--- a/src/main/java/org/apache/commons/crypto/utils/IOUtils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/IOUtils.java
@@ -21,7 +21,6 @@ import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.crypto.stream.input.Input;
-import org.apache.commons.logging.Log;
/**
* General utility methods for working with IO.
@@ -85,18 +84,15 @@ public final class IOUtils {
* Closes the Closeable objects and <b>ignore</b> any {@link IOException} or
* null pointers. Must only be used for cleanup in exception handlers.
*
- * @param log the log to record problems to at debug level. Can be null.
* @param closeables the objects to close.
*/
- public static void cleanup(Log log, java.io.Closeable... closeables) {
+ public static void cleanup(java.io.Closeable... closeables) {
for (java.io.Closeable c : closeables) {
if (c != null) {
try {
c.close();
} catch (Throwable e) {
- if (log != null && log.isDebugEnabled()) {
- log.debug("Exception in closing " + c, e);
- }
+ ; // NOPMD
}
}
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
index 59a102f..8738157 100644
--- a/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
+++ b/src/main/java/org/apache/commons/crypto/utils/NativeCodeLoader.java
@@ -27,9 +27,6 @@ import java.net.URL;
import java.util.Properties;
import java.util.UUID;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
/**
* A helper to load the native code i.e. libcommons-crypto.so. This handles the
* fallback to either the bundled libcommons-crypto-Linux-i386-32.so or the
@@ -37,8 +34,6 @@ import org.apache.commons.logging.LogFactory;
*/
public final class NativeCodeLoader {
- private static final Log LOG = LogFactory.getLog(NativeCodeLoader.class);
-
private final static boolean nativeCodeLoaded;
/**
* The private constructor of {@Link NativeCodeLoader}.
@@ -48,11 +43,9 @@ public final class NativeCodeLoader {
static {
// Try to load native library and set fallback flag appropriately
- if (LOG.isDebugEnabled()) {
- LOG.debug("Trying to load the custom-built native-commons-crypto library...");
- }
-
boolean nativeLoaded = false;
+
+ //Trying to load the custom-built native-commons-crypto library...");
try {
File nativeLibFile = findNativeLibrary();
if (nativeLibFile != null) {
@@ -62,22 +55,13 @@ public final class NativeCodeLoader {
// Load preinstalled library (in the path -Djava.library.path)
System.loadLibrary("commons-crypto");
}
- LOG.debug("Loaded the native library");
+ // Loaded the native library
nativeLoaded = true;
} catch (Throwable t) {
- // Ignore failure to load
- if (LOG.isDebugEnabled()) {
- LOG.debug("Failed to load native library with error: " + t);
- LOG.debug("java.library.path="
- + System.getProperty("java.library.path"));
- }
+ ;// NOPMD: Ignore failure to load
}
nativeCodeLoaded = nativeLoaded;
- if (!nativeCodeLoaded) {
- LOG.warn("Unable to load native library for the platform... "
- + "using builtin-java classes where applicable");
- }
}
/**
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/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 46212d5..0388a0e 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -275,7 +275,7 @@ public final class Utils {
int sum = 0;
while (i-- > 0) {
// (sum >>> Byte.SIZE) is the carry for addition
- sum = (initIV[i] & 0xff) + (sum >>> Byte.SIZE);
+ sum = (initIV[i] & 0xff) + (sum >>> Byte.SIZE); // NOPMD
if (j++ < 8) { // Big-endian, and long is 8 bytes length
sum += (byte) counter & 0xff;
counter >>>= 8;
@@ -385,4 +385,25 @@ public final class Utils {
}
return res;
}
+
+ /**
+ * Returns true if Fallback is enabled when native failed.
+ * @param props The <code>Properties</code> class represents a set of
+ * properties.
+ * @return true if Fallback is enabled when native failed.
+ */
+ public static boolean isFallbackEnable(Properties props) {
+ String enableFallback = props.getProperty(ConfigurationKeys.
+ COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY);
+ if (enableFallback == null || enableFallback.isEmpty()) {
+ enableFallback = System.getProperty(ConfigurationKeys.
+ COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY);
+ }
+ if (enableFallback == null || enableFallback.isEmpty()) {
+ return ConfigurationKeys
+ .COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_DEFAULT;
+ } else {
+ return Boolean.valueOf(enableFallback);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4117c569/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
index c54cfab..1f4b162 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
@@ -24,6 +24,7 @@ import org.apache.commons.crypto.conf.ConfigurationKeys;
import org.junit.Assert;
import org.junit.Test;
+import static junit.framework.Assert.fail;
public class CryptoCipherFactoryTest {
@Test
@@ -37,7 +38,8 @@ public class CryptoCipherFactoryTest {
@Test
public void testEmptyCipher() throws GeneralSecurityException {
Properties properties = new Properties();
- properties.put(ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY, "");
+ properties.setProperty(
+ ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY, "");
CryptoCipher defaultCipher = CryptoCipherFactory.getInstance(
CipherTransformation.AES_CBC_NOPADDING, properties);
Assert.assertEquals(OpensslCipher.class.getName(), defaultCipher
@@ -47,11 +49,28 @@ public class CryptoCipherFactoryTest {
@Test
public void testInvalidCipher() throws GeneralSecurityException {
Properties properties = new Properties();
- properties.put(ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY,
+ properties.setProperty(ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY,
"InvalidCipherName");
CryptoCipher defaultCipher = CryptoCipherFactory.getInstance(
CipherTransformation.AES_CBC_NOPADDING, properties);
Assert.assertEquals(JceCipher.class.getName(), defaultCipher.getClass()
.getName());
}
+
+ @Test
+ public void testDisableFallback() throws GeneralSecurityException {
+ Properties properties = new Properties();
+ properties.setProperty(
+ ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY,
+ "InvalidCipherName");
+ properties.setProperty(ConfigurationKeys
+ .COMMONS_CRYPTO_ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "false");
+ try {
+ CryptoCipher defaultCipher = CryptoCipherFactory.getInstance(
+ CipherTransformation.AES_CBC_NOPADDING, properties);
+ fail("Should throw an exception when DisableFallback");
+ } catch (Exception e) {
+ ;
+ }
+ }
}