You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/12/09 20:07:30 UTC
[commons-crypto] branch master updated: Refactor common code
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
The following commit(s) were added to refs/heads/master by this push:
new 22ad077 Refactor common code
22ad077 is described below
commit 22ad07739ef683dd56410b8e3d92a3982efd7a7d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Dec 9 15:07:25 2022 -0500
Refactor common code
---
pom.xml | 4 +--
.../java/org/apache/commons/crypto/Crypto.java | 6 ++--
.../commons/crypto/stream/CryptoInputStream.java | 5 ++--
.../crypto/stream/CtrCryptoInputStream.java | 8 +++---
.../crypto/stream/CtrCryptoOutputStream.java | 8 +++---
.../crypto/stream/PositionedCryptoInputStream.java | 5 ++--
.../java/org/apache/commons/crypto/utils/AES.java} | 31 +++++++++++---------
.../org/apache/commons/crypto/utils/Utils.java | 1 +
.../apache/commons/crypto/AbstractBenchmark.java | 6 ++--
.../commons/crypto/cipher/AbstractCipherTest.java | 33 +++++++++++++---------
.../crypto/cipher/CryptoCipherFactoryTest.java | 9 +++---
.../commons/crypto/cipher/GcmCipherTest.java | 32 +++++++++++----------
.../commons/crypto/cipher/JceCipherTest.java | 9 +++---
.../commons/crypto/cipher/OpenSslCipherTest.java | 25 ++++++++--------
.../org/apache/commons/crypto/cipher/TestData.java | 8 ++++--
.../crypto/examples/CipherByteArrayExample.java | 5 ++--
.../crypto/examples/CipherByteBufferExample.java | 5 ++--
.../commons/crypto/examples/StreamExample.java | 5 ++--
.../jna/CbcNoPaddingCipherJnaStreamTest.java | 3 +-
.../jna/CbcPkcs5PaddingCipherJnaStreamTest.java | 3 +-
.../commons/crypto/jna/CtrCryptoJnaStreamTest.java | 3 +-
.../jna/CtrNoPaddingCipherJnaStreamTest.java | 3 +-
.../commons/crypto/jna/OpenSslJnaCipherTest.java | 7 +++--
.../crypto/stream/AbstractCipherStreamTest.java | 27 +++++++++---------
.../stream/CbcNoPaddingCipherStreamTest.java | 4 ++-
.../stream/CbcPkcs5PaddingCipherStreamTest.java | 4 ++-
.../commons/crypto/stream/CtrCryptoStreamTest.java | 7 +++--
.../stream/CtrNoPaddingCipherStreamTest.java | 4 ++-
.../stream/PositionedCryptoInputStreamTest.java | 5 ++--
29 files changed, 156 insertions(+), 119 deletions(-)
diff --git a/pom.xml b/pom.xml
index f3c15dc..790659b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,7 @@
</parent>
<artifactId>commons-crypto</artifactId>
- <version>1.1.1-SNAPSHOT</version>
+ <version>1.2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Apache Commons Crypto</name>
@@ -110,7 +110,7 @@ The following provides more details on the included cryptographic software:
</distributionManagement>
<properties>
- <commons.release.version>1.1.1</commons.release.version>
+ <commons.release.version>1.2.0</commons.release.version>
<commons.release.desc>(Java 8)</commons.release.desc>
<!-- The RC version used in the staging repository URL. -->
<commons.rc.version>RC1</commons.rc.version>
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
index 31cabae..44c2191 100644
--- a/src/main/java/org/apache/commons/crypto/Crypto.java
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -26,6 +26,7 @@ import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;
+import org.apache.commons.crypto.utils.AES;
/**
* Provides diagnostic information about Commons Crypto and keys for native
@@ -172,9 +173,8 @@ public final class Crypto {
final Properties props = new Properties();
props.setProperty(CryptoCipherFactory.CLASSES_KEY,
CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
- final String transformation = "AES/CTR/NoPadding";
- try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher(transformation, props)) {
- info("Cipher %s instance created OK: %s", transformation, cryptoCipher);
+ try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher(AES.CTR_NO_PADDING, props)) {
+ info("Cipher %s instance created OK: %s", AES.CTR_NO_PADDING, cryptoCipher);
}
}
info("Additional OpenSSL_version(n) details:");
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 f22a22b..4c1d9ca 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
@@ -39,6 +39,7 @@ 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;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
@@ -632,8 +633,8 @@ public class CryptoInputStream extends InputStream implements
*/
static void checkStreamCipher(final CryptoCipher cipher)
throws IOException {
- if (!cipher.getAlgorithm().equals("AES/CTR/NoPadding")) {
- throw new IOException("AES/CTR/NoPadding is required");
+ if (!cipher.getAlgorithm().equals(AES.CTR_NO_PADDING)) {
+ throw new IOException(AES.CTR_NO_PADDING + " is required");
}
}
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 58712c4..5faed86 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CtrCryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CtrCryptoInputStream.java
@@ -26,13 +26,13 @@ import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.stream.input.ChannelInput;
import org.apache.commons.crypto.stream.input.Input;
import org.apache.commons.crypto.stream.input.StreamInput;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
@@ -167,7 +167,7 @@ public class CtrCryptoInputStream extends CryptoInputStream {
public CtrCryptoInputStream(final Properties properties, final InputStream inputStream, final byte[] key,
final byte[] iv, final long streamOffset) throws IOException {
this(inputStream, Utils.getCipherInstance(
- "AES/CTR/NoPadding", properties),
+ AES.CTR_NO_PADDING, properties),
CryptoInputStream.getBufferSize(properties), key, iv, streamOffset);
}
@@ -186,7 +186,7 @@ public class CtrCryptoInputStream extends CryptoInputStream {
public CtrCryptoInputStream(final Properties properties, final ReadableByteChannel in,
final byte[] key, final byte[] iv, final long streamOffset) throws IOException {
this(in, Utils.getCipherInstance(
- "AES/CTR/NoPadding", properties),
+ AES.CTR_NO_PADDING, properties),
CryptoInputStream.getBufferSize(properties), key, iv, streamOffset);
}
@@ -239,7 +239,7 @@ public class CtrCryptoInputStream extends CryptoInputStream {
protected CtrCryptoInputStream(final Input input, final CryptoCipher cipher,
final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset)
throws IOException {
- super(input, cipher, bufferSize, new SecretKeySpec(key, "AES"),
+ super(input, cipher, bufferSize, AES.newSecretKeySpec(key),
new IvParameterSpec(iv));
this.initIV = iv.clone();
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 c5a0ed8..e3a0fbd 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CtrCryptoOutputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CtrCryptoOutputStream.java
@@ -26,12 +26,12 @@ import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
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;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
@@ -172,7 +172,7 @@ public class CtrCryptoOutputStream extends CryptoOutputStream {
public CtrCryptoOutputStream(final Properties properties, final OutputStream outputStream,
final byte[] key, final byte[] iv, final long streamOffset) throws IOException {
this(outputStream, Utils.getCipherInstance(
- "AES/CTR/NoPadding", properties),
+ AES.CTR_NO_PADDING, properties),
CryptoInputStream.getBufferSize(properties), key, iv, streamOffset);
}
@@ -191,7 +191,7 @@ public class CtrCryptoOutputStream extends CryptoOutputStream {
public CtrCryptoOutputStream(final Properties properties, final WritableByteChannel channel,
final byte[] key, final byte[] iv, final long streamOffset) throws IOException {
this(channel, Utils.getCipherInstance(
- "AES/CTR/NoPadding", properties),
+ AES.CTR_NO_PADDING, properties),
CryptoInputStream.getBufferSize(properties), key, iv, streamOffset);
}
@@ -245,7 +245,7 @@ public class CtrCryptoOutputStream extends CryptoOutputStream {
protected CtrCryptoOutputStream(final Output output, final CryptoCipher cipher,
final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset)
throws IOException {
- super(output, cipher, bufferSize, new SecretKeySpec(key, "AES"),
+ super(output, cipher, bufferSize, AES.newSecretKeySpec(key),
new IvParameterSpec(iv));
CryptoInputStream.checkStreamCipher(cipher);
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 8837589..a40f9bd 100644
--- a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
@@ -30,6 +30,7 @@ import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.stream.input.Input;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.IoUtils;
import org.apache.commons.crypto.utils.Utils;
@@ -69,7 +70,7 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream {
@SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by PositionedCryptoInputStream.
public PositionedCryptoInputStream(final Properties properties, final Input in, final byte[] key,
final byte[] iv, final long streamOffset) throws IOException {
- this(properties, in, Utils.getCipherInstance("AES/CTR/NoPadding", properties),
+ this(properties, in, Utils.getCipherInstance(AES.CTR_NO_PADDING, properties),
CryptoInputStream.getBufferSize(properties), key, iv, streamOffset);
}
@@ -306,7 +307,7 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream {
if (state == null) {
final CryptoCipher cryptoCipher;
try {
- cryptoCipher = CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", properties);
+ cryptoCipher = CryptoCipherFactory.getCryptoCipher(AES.CTR_NO_PADDING, properties);
} catch (final GeneralSecurityException e) {
throw new IOException(e);
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java b/src/main/java/org/apache/commons/crypto/utils/AES.java
similarity index 58%
copy from src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
copy to src/main/java/org/apache/commons/crypto/utils/AES.java
index 37aab98..d7b6256 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
+++ b/src/main/java/org/apache/commons/crypto/utils/AES.java
@@ -1,4 +1,4 @@
- /*
+/*
* 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
@@ -15,23 +15,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package org.apache.commons.crypto.utils;
-package org.apache.commons.crypto.jna;
+import javax.crypto.spec.SecretKeySpec;
-import static org.junit.jupiter.api.Assumptions.assumeTrue;
+/**
+ * Creates AES objects
+ *
+ * @since 1.2.0
+ */
+public class AES {
-import org.apache.commons.crypto.cipher.AbstractCipherTest;
+ /** The AES algorithm name. */
+ public static final String ALGORITHM = "AES";
-public class OpenSslJnaCipherTest extends AbstractCipherTest {
+ public static final String CBC_NO_PADDING = "AES/CBC/NoPadding";
+ public static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
+ public static final String CTR_NO_PADDING = "AES/CTR/NoPadding";
- @Override
- public void init() {
- assumeTrue(OpenSslJna.isEnabled());
- transformations = new String[] {
- "AES/CBC/NoPadding",
- "AES/CBC/PKCS5Padding",
- "AES/CTR/NoPadding"
- };
- cipherClass = OpenSslJnaCipher.class.getName();
+ public static SecretKeySpec newSecretKeySpec(final byte[] key) {
+ return new SecretKeySpec(key, ALGORITHM);
}
+
}
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 d2d25bf..15e7c91 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -241,4 +241,5 @@ public final class Utils {
}
return name;
}
+
}
diff --git a/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
index 1f551ae..7d6dfe9 100644
--- a/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
+++ b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
@@ -28,6 +28,8 @@ import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;
+import org.apache.commons.crypto.utils.AES;
+import org.apache.commons.crypto.utils.Utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -37,7 +39,7 @@ public abstract class AbstractBenchmark {
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
private static final byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- private static final SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
+ private static final SecretKeySpec keySpec = Utils.newSecretKeySpec(KEY);
private static final IvParameterSpec ivSpec = new IvParameterSpec(IV);
private static final byte[] BUFFER = new byte[1000];
@@ -75,7 +77,7 @@ public abstract class AbstractBenchmark {
protected CryptoCipher getCipher(final String className) throws Exception {
final Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, className);
- final CryptoCipher cipher = CryptoCipherFactory.getCryptoCipher("AES/CBC/PKCS5Padding", properties);
+ final CryptoCipher cipher = CryptoCipherFactory.getCryptoCipher(AES.CTR_NO_PADDING, properties);
assertEquals(className, cipher.getClass().getCanonicalName());
return cipher;
}
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 01f807f..735bc84 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -37,6 +37,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.ReflectionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -87,18 +88,22 @@ public abstract class AbstractCipherTest {
// This test deliberately does not use try with resources in order to control
// the sequence of operations exactly
try (final CryptoCipher enc = getCipher(transformations[0])) {
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
+ enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
}
}
+ SecretKeySpec newSecretKeySpec() {
+ return AES.newSecretKeySpec(KEY);
+ }
+
@Test
public void reInitTest() throws Exception {
// This test deliberately does not use try with resources in order to control
// the sequence of operations exactly
try (final CryptoCipher enc = getCipher(transformations[0])) {
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
- enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
+ enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
+ enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
+ enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
}
}
@@ -107,9 +112,9 @@ public abstract class AbstractCipherTest {
// This test deliberately does not use try with resources in order to control
// the sequence of operations exactly
try (final CryptoCipher enc = getCipher(transformations[0])) {
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
+ enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
enc.close();
- enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(IV));
+ enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV));
}
}
@@ -172,7 +177,7 @@ public abstract class AbstractCipherTest {
final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
- assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(invalidKey, "AES"), new IvParameterSpec(IV)));
+ assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, AES.newSecretKeySpec(invalidKey), new IvParameterSpec(IV)));
}
}
}
@@ -184,7 +189,7 @@ public abstract class AbstractCipherTest {
assertNotNull(cipher);
final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f, 0x11 };
- assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(invalidIV)));
+ assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(invalidIV)));
}
}
}
@@ -194,7 +199,7 @@ public abstract class AbstractCipherTest {
for (final String transform : transformations) {
try (final CryptoCipher cipher = getCipher(transform)) {
assertNotNull(cipher);
- assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new GCMParameterSpec(IV.length, IV)));
+ assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, newSecretKeySpec(), new GCMParameterSpec(IV.length, IV)));
}
}
}
@@ -206,8 +211,8 @@ public abstract class AbstractCipherTest {
try (final CryptoCipher enc = getCipher(transformation); final CryptoCipher dec = getCipher(transformation)) {
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
- dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
+ enc.init(Cipher.ENCRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv));
+ dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv));
//
// encryption pass
@@ -265,7 +270,7 @@ public abstract class AbstractCipherTest {
// AES_CBC_NOPADDING only accepts data whose size is the multiple of
// block size
- final int[] dataLenList = transformation.equals("AES/CBC/NoPadding") ? new int[] { 10 * 1024 }
+ final int[] dataLenList = transformation.equals(AES.CBC_NO_PADDING) ? new int[] { 10 * 1024 }
: new int[] { 10 * 1024, 10 * 1024 - 3 };
for (final int dataLen : dataLenList) {
final byte[] plainText = new byte[dataLen];
@@ -313,9 +318,9 @@ public abstract class AbstractCipherTest {
enc = getCipher(transformation);
dec = getCipher(transformation);
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
+ enc.init(Cipher.ENCRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv));
- dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
+ dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv));
}
private CryptoCipher getCipher(final String transformation) throws ClassNotFoundException {
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 1ce6148..3132b0d 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.security.GeneralSecurityException;
import java.util.Properties;
+import org.apache.commons.crypto.utils.AES;
import org.junit.jupiter.api.Test;
@@ -30,7 +31,7 @@ public class CryptoCipherFactoryTest {
@Test
public void testDefaultCipher() throws GeneralSecurityException {
final CryptoCipher defaultCipher = CryptoCipherFactory
- .getCryptoCipher("AES/CBC/NoPadding");
+ .getCryptoCipher(AES.CTR_NO_PADDING);
final String name = defaultCipher.getClass().getName();
if (OpenSsl.getLoadingFailureReason() == null) {
assertEquals(OpenSslCipher.class.getName(), name);
@@ -44,7 +45,7 @@ public class CryptoCipherFactoryTest {
final Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ""); // TODO should this really mean use the default?
final CryptoCipher defaultCipher = CryptoCipherFactory.getCryptoCipher(
- "AES/CBC/NoPadding", properties);
+ AES.CBC_NO_PADDING, properties);
final String name = defaultCipher.getClass().getName();
if (OpenSsl.getLoadingFailureReason() == null) {
assertEquals(OpenSslCipher.class.getName(), name);
@@ -59,7 +60,7 @@ public class CryptoCipherFactoryTest {
properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
"InvalidCipherName");
assertThrows(GeneralSecurityException.class,
- () -> CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties));
+ () -> CryptoCipherFactory.getCryptoCipher(AES.CBC_NO_PADDING, properties));
}
@@ -78,7 +79,7 @@ public class CryptoCipherFactoryTest {
// However the splitter drops empty fields
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ",");
assertThrows(IllegalArgumentException.class,
- () -> CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties));
+ () -> CryptoCipherFactory.getCryptoCipher(AES.CBC_NO_PADDING, properties));
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
index f36ab44..35ce2cf 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
@@ -33,6 +33,7 @@ import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -40,9 +41,10 @@ import org.junit.jupiter.api.Test;
public class GcmCipherTest {
+ private static final String GCM_NO_PADDING = "AES/GCM/NoPadding";
Properties props;
String cipherClass;
- String transformation = "AES/GCM/NoPadding";
+ String transformation = GCM_NO_PADDING;
private String[] kHex;
private String[] pHex;
@@ -222,7 +224,7 @@ public class GcmCipherTest {
final byte[] decOutput = new byte[plainBytes.length];
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(tagLength, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
@@ -234,7 +236,7 @@ public class GcmCipherTest {
encOutput[0] = (byte)(encOutput[0] + 1);
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(tagLength, ivBytes);
c.init(Cipher.DECRYPT_MODE, key, iv);
@@ -268,7 +270,7 @@ public class GcmCipherTest {
{
final Cipher c = Cipher.getInstance(transformation);
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
c.updateAAD(aad);
@@ -276,7 +278,7 @@ public class GcmCipherTest {
}
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
c.updateAAD(aad);
@@ -289,7 +291,7 @@ public class GcmCipherTest {
// like JDK's decrypt mode. The plaintext+tag is the input for decrypt mode
// let's verify the add & tag now
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.DECRYPT_MODE, key, iv);
c.updateAAD(aad);
@@ -315,7 +317,7 @@ public class GcmCipherTest {
r.nextBytes(aad);
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
c.updateAAD(aad);
@@ -324,7 +326,7 @@ public class GcmCipherTest {
// like JDK's decrypt mode. The plaintext+tag is the input for decrypt mode
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.DECRYPT_MODE, key, iv);
@@ -351,7 +353,7 @@ public class GcmCipherTest {
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
@@ -380,7 +382,7 @@ public class GcmCipherTest {
int partLen;
int len;
try (final CryptoCipher enc = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
enc.init(Cipher.ENCRYPT_MODE, key, iv);
if (aad.length > 0) {
@@ -402,7 +404,7 @@ public class GcmCipherTest {
// Decryption
try (final CryptoCipher dec = Utils.getCipherInstance(transformation, props)) {
- dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new GCMParameterSpec(128, ivBytes));
+ dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(keyBytes), new GCMParameterSpec(128, ivBytes));
if (aad.length > 0) {
final int len1 = r.nextInt(aad.length);
final byte[] aad1 = Arrays.copyOfRange(aad, 0, len1);
@@ -436,7 +438,7 @@ public class GcmCipherTest {
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.DECRYPT_MODE, key, iv);
@@ -462,7 +464,7 @@ public class GcmCipherTest {
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.DECRYPT_MODE, key, iv);
@@ -500,7 +502,7 @@ public class GcmCipherTest {
// Encryption -------------------
try (final CryptoCipher c = Utils.getCipherInstance(transformation, props)) {
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final Key key = AES.newSecretKeySpec(keyBytes);
final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
c.init(Cipher.ENCRYPT_MODE, key, iv);
@@ -520,7 +522,7 @@ public class GcmCipherTest {
// Decryption -------------------
try (final CryptoCipher dec = Utils.getCipherInstance(transformation, props)) {
- dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new GCMParameterSpec(128, ivBytes));
+ dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(keyBytes), new GCMParameterSpec(128, ivBytes));
bfAAD.flip();
dec.updateAAD(bfAAD);
bfCipherText.clear();
diff --git a/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
index 3fd959b..81f39ba 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
@@ -24,6 +24,7 @@ import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
+import org.apache.commons.crypto.utils.AES;
import org.junit.jupiter.api.BeforeAll;
@@ -34,15 +35,15 @@ public class JceCipherTest extends AbstractCipherTest {
@Override
public void init() {
transformations = new String[] {
- "AES/CBC/NoPadding",
- "AES/CBC/PKCS5Padding",
- "AES/CTR/NoPadding"};
+ AES.CBC_NO_PADDING,
+ AES.CBC_PKCS5_PADDING,
+ AES.CTR_NO_PADDING};
cipherClass = JCE_CIPHER_CLASSNAME;
}
@BeforeAll
public static void checkJceUnlimitedStrength() throws NoSuchAlgorithmException {
- final int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
+ final int maxKeyLen = Cipher.getMaxAllowedKeyLength(AES.ALGORITHM);
assertTrue(maxKeyLen >= MAX_KEY_LEN_LOWER_BOUND,
String.format(
"Testing requires support for an AES key length of %d, but " +
diff --git a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
index 69a7f37..a234e89 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
@@ -36,6 +36,7 @@ import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
+import org.apache.commons.crypto.utils.AES;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
@@ -46,9 +47,9 @@ public class OpenSslCipherTest extends AbstractCipherTest {
public void init() {
assumeTrue(OpenSsl.getLoadingFailureReason() == null);
transformations = new String[] {
- "AES/CBC/NoPadding",
- "AES/CBC/PKCS5Padding",
- "AES/CTR/NoPadding"};
+ AES.CBC_NO_PADDING,
+ AES.CBC_PKCS5_PADDING,
+ AES.CTR_NO_PADDING};
cipherClass = OPENSSL_CIPHER_CLASSNAME;
}
@@ -73,7 +74,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
public void testUpdateArguments() throws Exception {
assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
- .getInstance("AES/CTR/NoPadding");
+ .getInstance(AES.CTR_NO_PADDING);
assertNotNull(cipher);
cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(IV));
@@ -102,7 +103,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
public void testDoFinalArguments() throws Exception {
assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
- .getInstance("AES/CTR/NoPadding");
+ .getInstance(AES.CTR_NO_PADDING);
assertNotNull(cipher);
cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(IV));
@@ -121,7 +122,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
public void testInvalidKey() throws Exception {
assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
- .getInstance("AES/CTR/NoPadding");
+ .getInstance(AES.CTR_NO_PADDING);
assertNotNull(cipher);
final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
@@ -137,7 +138,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
public void testInvalidIV() throws Exception {
assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
- .getInstance("AES/CTR/NoPadding");
+ .getInstance(AES.CTR_NO_PADDING);
assertNotNull(cipher);
final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
@@ -151,7 +152,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
@Test
@Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testInvalidIVClass() throws Exception {
- final OpenSsl cipher = OpenSsl.getInstance("AES/CTR/NoPadding");
+ final OpenSsl cipher = OpenSsl.getInstance(AES.CTR_NO_PADDING);
assertNotNull(cipher);
@@ -161,14 +162,14 @@ public class OpenSslCipherTest extends AbstractCipherTest {
@Test
public void testCipherLifecycle() throws Exception {
- try (OpenSslCipher cipher = new OpenSslCipher(new Properties(), "AES/CTR/NoPadding")) {
+ try (OpenSslCipher cipher = new OpenSslCipher(new Properties(), AES.CTR_NO_PADDING)) {
assertThrows(IllegalStateException.class, () -> cipher.update(dummyBuffer(), dummyBuffer()));
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
+ cipher.init(OpenSsl.ENCRYPT_MODE, AES.newSecretKeySpec(KEY),
new IvParameterSpec(IV));
cipher.update(dummyBuffer(), dummyBuffer());
- assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(new byte[1], "AES"),
+ assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, AES.newSecretKeySpec(new byte[1]),
new IvParameterSpec(IV)));
// Should keep working with previous init parameters.
cipher.update(dummyBuffer(), dummyBuffer());
@@ -176,7 +177,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
cipher.close();
assertThrows(IllegalStateException.class, () -> cipher.update(dummyBuffer(), dummyBuffer()));
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
+ cipher.init(OpenSsl.ENCRYPT_MODE, AES.newSecretKeySpec(KEY),
new IvParameterSpec(IV));
cipher.update(dummyBuffer(), dummyBuffer());
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/TestData.java b/src/test/java/org/apache/commons/crypto/cipher/TestData.java
index f0f55d4..17893fc 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/TestData.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/TestData.java
@@ -20,6 +20,8 @@ package org.apache.commons.crypto.cipher;
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.crypto.utils.AES;
+
public class TestData {
private static final String[] CBCNoPaddingTests = {
@@ -139,9 +141,9 @@ public class TestData {
private static final Map<String, String[]> testData = new HashMap<>();
static {
- testData.put("AES/CBC/NoPadding", CBCNoPaddingTests);
- testData.put("AES/CBC/PKCS5Padding", CBCPKCS5PaddingTests);
- testData.put("AES/CTR/NoPadding", cipherCTRTests);
+ testData.put(AES.CBC_NO_PADDING, CBCNoPaddingTests);
+ testData.put(AES.CBC_PKCS5_PADDING, CBCPKCS5PaddingTests);
+ testData.put(AES.CTR_NO_PADDING, cipherCTRTests);
}
public static String[] getTestData(final String transformation) {
diff --git a/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
index 15afb8d..a230134 100644
--- a/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
+++ b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
@@ -28,6 +28,7 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
@@ -37,13 +38,13 @@ public class CipherByteArrayExample {
public static void main(final String[] args) throws Exception {
- final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
+ final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
final Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
// Creates a CryptoCipher instance with the transformation and properties.
- final String transform = "AES/CBC/PKCS5Padding";
+ final String transform = AES.CBC_PKCS5_PADDING;
byte[] output;
int updateBytes;
int finalBytes;
diff --git a/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java b/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
index ebb4ed9..7dd241e 100644
--- a/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
+++ b/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
@@ -27,6 +27,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
@@ -35,11 +36,11 @@ import org.apache.commons.crypto.utils.Utils;
public class CipherByteBufferExample {
public static void main(final String[] args) throws Exception {
- final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
+ final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
final Properties properties = new Properties();
//Creates a CryptoCipher instance with the transformation and properties.
- final String transform = "AES/CBC/PKCS5Padding";
+ final String transform = AES.CBC_PKCS5_PADDING;
final ByteBuffer outBuffer;
final int bufferSize = 1024;
final int updateBytes;
diff --git a/src/test/java/org/apache/commons/crypto/examples/StreamExample.java b/src/test/java/org/apache/commons/crypto/examples/StreamExample.java
index aeef592..5b7be6e 100644
--- a/src/test/java/org/apache/commons/crypto/examples/StreamExample.java
+++ b/src/test/java/org/apache/commons/crypto/examples/StreamExample.java
@@ -30,6 +30,7 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.stream.CryptoInputStream;
import org.apache.commons.crypto.stream.CryptoOutputStream;
+import org.apache.commons.crypto.utils.AES;
/**
* Example showing how to use stream encryption and decryption.
@@ -37,10 +38,10 @@ import org.apache.commons.crypto.stream.CryptoOutputStream;
public class StreamExample {
public static void main(final String []args) throws IOException {
- final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES");
+ final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
final Properties properties = new Properties();
- final String transform = "AES/CBC/PKCS5Padding";
+ final String transform = AES.CBC_PKCS5_PADDING;
final String input = "hello world!";
//Encryption with CryptoOutputStream.
diff --git a/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
index deba4b8..69439e3 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
@@ -17,11 +17,12 @@
*/
package org.apache.commons.crypto.jna;
+import org.apache.commons.crypto.utils.AES;
public class CbcNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
public void setUp() {
- transformation = "AES/CBC/NoPadding";
+ transformation = AES.CBC_NO_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
index 144dcb5..2d78e2a 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
@@ -17,12 +17,13 @@
*/
package org.apache.commons.crypto.jna;
+import org.apache.commons.crypto.utils.AES;
public class CbcPkcs5PaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
public void setUp() {
- transformation = "AES/CBC/PKCS5Padding";
+ transformation = AES.CBC_PKCS5_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
index 137afb4..e82b1ee 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
@@ -17,12 +17,13 @@
*/
package org.apache.commons.crypto.jna;
+import org.apache.commons.crypto.utils.AES;
public class CtrCryptoJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
public void setUp() {
- transformation = "AES/CTR/NoPadding";
+ transformation = AES.CTR_NO_PADDING;
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
index 863fc1b..123cd7c 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
@@ -17,12 +17,13 @@
*/
package org.apache.commons.crypto.jna;
+import org.apache.commons.crypto.utils.AES;
public class CtrNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
public void setUp() {
- transformation = "AES/CTR/NoPadding";
+ transformation = AES.CTR_NO_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
index 37aab98..6bdf9ef 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
@@ -21,6 +21,7 @@ package org.apache.commons.crypto.jna;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
+import org.apache.commons.crypto.utils.AES;
public class OpenSslJnaCipherTest extends AbstractCipherTest {
@@ -28,9 +29,9 @@ public class OpenSslJnaCipherTest extends AbstractCipherTest {
public void init() {
assumeTrue(OpenSslJna.isEnabled());
transformations = new String[] {
- "AES/CBC/NoPadding",
- "AES/CBC/PKCS5Padding",
- "AES/CTR/NoPadding"
+ AES.CBC_NO_PADDING,
+ AES.CBC_PKCS5_PADDING,
+ AES.CTR_NO_PADDING
};
cipherClass = OpenSslJnaCipher.class.getName();
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
index 600dfae..4b77812 100644
--- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -49,6 +49,7 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.Crypto;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.cipher.CryptoCipher;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.ReflectionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -349,17 +350,17 @@ public abstract class AbstractCipherStreamTest {
// Test InvalidAlgorithmParameters
Exception ex = assertThrows(IOException.class, () -> newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData),
- new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), withChannel));
+ AES.newSecretKeySpec(key), new GCMParameterSpec(0, new byte[0]), withChannel));
assertEquals(ex.getMessage(),"Illegal parameters");
// Test InvalidAlgorithmParameters
ex = assertThrows(IOException.class, () -> newCryptoOutputStream(transformation, props, baos,
- new SecretKeySpec(key, "AES"), new GCMParameterSpec(0,
+ AES.newSecretKeySpec(key), new GCMParameterSpec(0,
new byte[0]), withChannel));
assertEquals(ex.getMessage(),"Illegal parameters");
// Test Invalid Key
assertThrows(IOException.class, () -> newCryptoInputStream(transformation,props, new ByteArrayInputStream(encData),
- new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel));
+ AES.newSecretKeySpec(new byte[10]), new IvParameterSpec(iv), withChannel));
// Test Invalid Key
assertThrows(IOException.class, () -> newCryptoOutputStream(transformation, props, baos, new byte[10],
new IvParameterSpec(iv), withChannel));
@@ -432,7 +433,7 @@ public abstract class AbstractCipherStreamTest {
assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
assertEquals(in.getBufferSize(), defaultBufferSize);
assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
- assertEquals(in.getKey().getAlgorithm(), "AES");
+ assertEquals(in.getKey().getAlgorithm(), AES.ALGORITHM);
assertEquals(in.getParams().getClass(), IvParameterSpec.class);
assertNotNull(in.getInput());
@@ -489,7 +490,7 @@ public abstract class AbstractCipherStreamTest {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (OutputStream out = new CryptoOutputStream(baos, cipher,
- defaultBufferSize, new SecretKeySpec(key, "AES"),
+ defaultBufferSize, AES.newSecretKeySpec(key),
new IvParameterSpec(iv))) {
out.write(data);
out.flush();
@@ -533,20 +534,20 @@ public abstract class AbstractCipherStreamTest {
throws IOException {
if (withChannel) {
return new CryptoInputStream(Channels.newChannel(bais), cipher,
- bufferSize, new SecretKeySpec(key, "AES"),
+ bufferSize, AES.newSecretKeySpec(key),
new IvParameterSpec(iv));
}
return new CryptoInputStream(bais, cipher, bufferSize,
- new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
+ AES.newSecretKeySpec(key), new IvParameterSpec(iv));
}
protected CryptoInputStream newCryptoInputStream(final String transformation, final Properties props,
final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
final boolean withChannel) throws IOException {
if (withChannel) {
- return new CryptoInputStream(transformation, props, Channels.newChannel(bais), new SecretKeySpec(key, "AES"), params);
+ return new CryptoInputStream(transformation, props, Channels.newChannel(bais), AES.newSecretKeySpec(key), params);
}
- return new CryptoInputStream(transformation, props, bais, new SecretKeySpec(key, "AES"), params);
+ return new CryptoInputStream(transformation, props, bais, AES.newSecretKeySpec(key), params);
}
protected CryptoInputStream newCryptoInputStream(final String transformation,
@@ -563,11 +564,11 @@ public abstract class AbstractCipherStreamTest {
final byte[] iv, final boolean withChannel) throws IOException {
if (withChannel) {
return new CryptoOutputStream(Channels.newChannel(baos), cipher,
- bufferSize, new SecretKeySpec(key, "AES"),
+ bufferSize, AES.newSecretKeySpec(key),
new IvParameterSpec(iv));
}
return new CryptoOutputStream(baos, cipher, bufferSize,
- new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
+ AES.newSecretKeySpec(key), new IvParameterSpec(iv));
}
protected CryptoOutputStream newCryptoOutputStream(final String transformation,
@@ -575,9 +576,9 @@ public abstract class AbstractCipherStreamTest {
final AlgorithmParameterSpec param, final boolean withChannel) throws IOException {
if (withChannel) {
return new CryptoOutputStream(transformation, props, Channels.newChannel(baos),
- new SecretKeySpec(key, "AES"), param);
+ AES.newSecretKeySpec(key), param);
}
- return new CryptoOutputStream(transformation, props, baos, new SecretKeySpec(key, "AES"),
+ return new CryptoOutputStream(transformation, props, baos, AES.newSecretKeySpec(key),
param);
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
index 1dc9976..3aa6dee 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
@@ -17,11 +17,13 @@
*/
package org.apache.commons.crypto.stream;
+import org.apache.commons.crypto.utils.AES;
+
public class CbcNoPaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
public void setUp() {
- transformation = "AES/CBC/NoPadding";
+ transformation = AES.CBC_NO_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
index ea90283..57a0445 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
@@ -17,10 +17,12 @@
*/
package org.apache.commons.crypto.stream;
+import org.apache.commons.crypto.utils.AES;
+
public class CbcPkcs5PaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
public void setUp() {
- transformation = "AES/CBC/PKCS5Padding";
+ transformation = AES.CBC_PKCS5_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
index 894ab83..90173c2 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
@@ -40,6 +40,7 @@ import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.stream.input.ChannelInput;
import org.apache.commons.crypto.stream.input.StreamInput;
import org.apache.commons.crypto.stream.output.ChannelOutput;
+import org.apache.commons.crypto.utils.AES;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
@@ -47,7 +48,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
@Override
public void setUp() {
- transformation = "AES/CTR/NoPadding";
+ transformation = AES.CTR_NO_PADDING;
}
@Override
@@ -62,7 +63,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
}
@Override
- protected CtrCryptoInputStream newCryptoInputStream(final String transformation, final Properties props,
+ protected CryptoInputStream newCryptoInputStream(final String transformation, final Properties props,
final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
final boolean withChannel) throws IOException {
if (withChannel) {
@@ -130,7 +131,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
assertEquals(smallBufferSize, in.getStreamOffset());
assertEquals(in.getBufferSize(), 8192);
assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
- assertEquals(in.getKey().getAlgorithm(), "AES");
+ assertEquals(in.getKey().getAlgorithm(), AES.ALGORITHM);
assertEquals(in.getParams().getClass(), IvParameterSpec.class);
assertNotNull(in.getInput());
diff --git a/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
index e7358ac..0c0c7f9 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
@@ -17,11 +17,13 @@
*/
package org.apache.commons.crypto.stream;
+import org.apache.commons.crypto.utils.AES;
+
public class CtrNoPaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
public void setUp() {
- transformation = "AES/CTR/NoPadding";
+ transformation = AES.CTR_NO_PADDING;
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
index 8bc0dec..c6bfff7 100644
--- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -40,6 +40,7 @@ import org.apache.commons.crypto.Crypto;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.stream.input.Input;
+import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.ReflectionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -59,7 +60,7 @@ public class PositionedCryptoInputStreamTest {
private final int lengthLess = length - 1;
private final int lengthMore = length + 1;
- private final String transformation = "AES/CTR/NoPadding";
+ private final String transformation = AES.CTR_NO_PADDING;
@BeforeEach
public void before() throws IOException {
@@ -83,7 +84,7 @@ public class PositionedCryptoInputStreamTest {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// encryption data
try (final OutputStream out = new CryptoOutputStream(baos, cipher, bufferSize,
- new SecretKeySpec(key, "AES"), new IvParameterSpec(iv))) {
+ AES.newSecretKeySpec(key), new IvParameterSpec(iv))) {
out.write(testData);
out.flush();
}