Repository: commons-crypto Updated Branches: refs/heads/master 42f541f39 -> c8829a514
CRYPTO-74: Full class names make code more difficult to update Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/c8829a51 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/c8829a51 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/c8829a51 Branch: refs/heads/master Commit: c8829a5142708eac5bdf2237b18d7c4badb130d0 Parents: 42f541f Author: Sun Dapeng <[email protected]> Authored: Wed Jun 29 11:31:26 2016 +0800 Committer: Sun Dapeng <[email protected]> Committed: Wed Jun 29 13:03:23 2016 +0800 ---------------------------------------------------------------------- .../crypto/cipher/CryptoCipherFactory.java | 54 ++++++++++++++++++-- .../crypto/examples/CipherByteArrayExample.java | 10 +++- .../commons/crypto/examples/RandomExample.java | 2 +- .../crypto/random/CryptoRandomFactory.java | 49 ++++++++++++++++++ 4 files changed, 108 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c8829a51/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 55a3203..8ba54c6 100644 --- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java +++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java @@ -30,10 +30,56 @@ import org.apache.commons.crypto.utils.Utils; public class CryptoCipherFactory { /** + * Defines the internal CryptoCipher implementations. + * <p> + * Usage: + * <p> + * <code> + * props.setProperty(CIPHER_CLASSES_KEY, CipherProvider.OPENSSL.getClassName()); + * </code> + */ + public enum CipherProvider { + + OPENSSL(OpensslCipher.class), + JCE(JceCipher.class); + + private final Class<? extends CryptoCipher> klass; + + private final String className; + + /** + * Constructs a CihpherProvider. + * + * @param klass the implementation of provider + */ + private CipherProvider(Class<? extends CryptoCipher> klass) { + this.klass = klass; + this.className = klass.getName(); + } + + /** + * Gets the class name of the provider. + * + * @return the name of the provider class + */ + public String getClassName() { + return className; + } + + /** + * Gets the implementation class of the provider. + * + * @return the implementation class of the provider + */ + public Class<? extends CryptoCipher> getImplClass() { + return klass; + } + } + + /** * The default value (OpensslCipher) for crypto cipher. */ - private static final String CIPHER_CLASSES_DEFAULT = - OpensslCipher.class.getName(); + private static final String CIPHER_CLASSES_DEFAULT = CipherProvider.OPENSSL.getClassName(); /** * The private Constructor of {@link CryptoCipherFactory}. @@ -44,7 +90,7 @@ public class CryptoCipherFactory { /** * Gets a cipher instance for specified algorithm/mode/padding. * - * @param props the configuration properties + * @param props the configuration properties * (uses ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY and ConfigurationKeys.CIPHER_CLASSES_KEY) * @param transformation algorithm/mode/padding * @return CryptoCipher the cipher (defaults to OpensslCipher if available, else JceCipher) @@ -52,7 +98,7 @@ public class CryptoCipherFactory { * @throws IllegalArgumentException if no classname(s) are provided and fallback is disabled */ public static CryptoCipher getInstance(String transformation, - Properties props) throws GeneralSecurityException { + Properties props) throws GeneralSecurityException { CryptoCipher cipher = null; http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c8829a51/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java b/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java index f36ab58..784b722 100644 --- a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java @@ -26,7 +26,9 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.crypto.cipher.CryptoCipher; +import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider; import org.apache.commons.crypto.utils.Utils; +import static org.apache.commons.crypto.conf.ConfigurationKeys.CIPHER_CLASSES_KEY; /** * Example showing use of the CryptoCipher API using a byte array @@ -39,6 +41,7 @@ public class CipherByteArrayExample { final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); Properties properties = new Properties(); + properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.OPENSSL.getClassName()); //Creates a CryptoCipher instance with the transformation and properties. final String transform = "AES/CBC/PKCS5Padding"; CryptoCipher encipher = Utils.getCipherInstance(transform, properties); @@ -48,7 +51,7 @@ public class CipherByteArrayExample { System.out.println("input: " + sampleInput); byte[] input = getUTF8Bytes(sampleInput); - byte[] output = new byte[32]; + byte[] output = new byte[32]; //Initializes the cipher with ENCRYPT_MODE, key and iv. encipher.init(Cipher.ENCRYPT_MODE, key, iv); @@ -63,8 +66,11 @@ public class CipherByteArrayExample { System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes+finalBytes))); - // Now reverse the process + // Now reverse the process using a different implementation with the same settings + properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.JCE.getClassName()); CryptoCipher decipher = Utils.getCipherInstance(transform, properties); + System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); + decipher.init(Cipher.DECRYPT_MODE, key, iv); byte [] decoded = new byte[32]; decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c8829a51/src/main/java/org/apache/commons/crypto/examples/RandomExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java b/src/main/java/org/apache/commons/crypto/examples/RandomExample.java index 7b19b62..2a9ff41 100644 --- a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/RandomExample.java @@ -38,7 +38,7 @@ public class RandomExample { Properties properties = new Properties(); properties.put(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, - "org.apache.commons.crypto.random.OpensslCryptoRandom"); // TODO replace with alias + CryptoRandomFactory.RandomProvider.OPENSSL.getClassName()); //Gets the 'CryptoRandom' instance. CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c8829a51/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 4b9fc58..899a908 100644 --- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java +++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java @@ -30,6 +30,55 @@ import static org.apache.commons.crypto.conf.ConfigurationKeys.SECURE_RANDOM_CLA public class CryptoRandomFactory { /** + * Defines the internal CryptoRandom implementations. + * <p> + * Usage: + * <p> + * <code> + * props.setProperty(RANDOM_CLASSES_KEY, + * CipherProvider.OPENSSL.getClassName()); + * </code> + */ + public enum RandomProvider { + + OPENSSL(OpensslCryptoRandom.class), + JCE(JavaCryptoRandom.class), + OS(OsCryptoRandom.class); + + private final Class<? extends CryptoRandom> klass; + + private final String className; + + /** + * Constructs a RandomProvider. + * + * @param klass the implementation of provider + */ + private RandomProvider(Class<? extends CryptoRandom> klass) { + this.klass = klass; + this.className = klass.getName(); + } + + /** + * Gets the class name of the provider. + * + * @return the name of the provider class + */ + public String getClassName() { + return className; + } + + /** + * Gets the implementation class of the provider. + * + * @return the implementation class of the provider + */ + public Class<? extends CryptoRandom> getImplClass() { + return klass; + } + } + + /** * The private constructor of {@Link CryptoRandomFactory}. */ private CryptoRandomFactory() {
