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 4f2b74b Lots of clean ups. 4f2b74b is described below commit 4f2b74b7dcb439ae323b12b87682557910890947 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Aug 5 17:04:30 2020 -0400 Lots of clean ups. - Make the private inner class CipherState static. - Better method names in tests. - Use try-with-resources. - Don't use deprecated methods. - Better param names. - Use transient where needed. --- .../apache/commons/crypto/NativeCodeLoader.java | 39 +++----- .../commons/crypto/cipher/CryptoCipherFactory.java | 11 ++- .../commons/crypto/jna/OpenSslJnaCryptoRandom.java | 3 +- .../commons/crypto/random/JavaCryptoRandom.java | 5 + .../commons/crypto/stream/CryptoInputStream.java | 32 +++---- .../commons/crypto/stream/CryptoOutputStream.java | 24 ++--- .../crypto/stream/CtrCryptoInputStream.java | 60 ++++++------ .../crypto/stream/CtrCryptoOutputStream.java | 30 +++--- .../crypto/stream/PositionedCryptoInputStream.java | 21 +++-- .../org/apache/commons/crypto/utils/Utils.java | 6 +- .../commons/crypto/cipher/AbstractCipherTest.java | 95 +++++++++---------- .../crypto/stream/AbstractCipherStreamTest.java | 102 ++++++++++----------- .../commons/crypto/stream/CtrCryptoStreamTest.java | 10 +- 13 files changed, 215 insertions(+), 223 deletions(-) diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java index b4b6d9e..e182fc3 100644 --- a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java +++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java @@ -140,44 +140,37 @@ final class NativeCodeLoader { * @return the library file. */ private static File extractLibraryFile(final String libFolderForCurrentOS, - final String libraryFileName, final String targetFolder) { - final String nativeLibraryFilePath = libFolderForCurrentOS + "/" - + libraryFileName; + final String libraryFileName, final String targetFolder) { + final String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName; // Attach UUID to the native library file to ensure multiple class // loaders // can read the libcommons-crypto multiple times. final String uuid = UUID.randomUUID().toString(); - final String extractedLibFileName = String.format("commons-crypto-%s-%s", - uuid, libraryFileName); + final String extractedLibFileName = String.format("commons-crypto-%s-%s", uuid, libraryFileName); final File extractedLibFile = new File(targetFolder, extractedLibFileName); - InputStream reader = null; + InputStream inputStream = null; try { // Extract a native library file into the target directory - reader = NativeCodeLoader.class - .getResourceAsStream(nativeLibraryFilePath); - final FileOutputStream writer = new FileOutputStream(extractedLibFile); - try { + inputStream = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath); + try (FileOutputStream writer = new FileOutputStream(extractedLibFile)) { final byte[] buffer = new byte[8192]; int bytesRead; - while ((bytesRead = reader.read(buffer)) != -1) { + while ((bytesRead = inputStream.read(buffer)) != -1) { writer.write(buffer, 0, bytesRead); } } finally { // Delete the extracted lib file on JVM exit. extractedLibFile.deleteOnExit(); - writer.close(); - - IoUtils.closeQuietly(reader); - reader = null; + IoUtils.closeQuietly(inputStream); + inputStream = null; } // Set executable (x) flag to enable Java to load the native library - if (!extractedLibFile.setReadable(true) - || !extractedLibFile.setExecutable(true) - || !extractedLibFile.setWritable(true, true)) { + if (!extractedLibFile.setReadable(true) || !extractedLibFile.setExecutable(true) + || !extractedLibFile.setWritable(true, true)) { throw new RuntimeException("Invalid path for library path"); } @@ -187,13 +180,11 @@ final class NativeCodeLoader { InputStream nativeIn = null; InputStream extractedLibIn = null; try { - nativeIn = NativeCodeLoader.class - .getResourceAsStream(nativeLibraryFilePath); + nativeIn = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath); extractedLibIn = new FileInputStream(extractedLibFile); if (!contentsEquals(nativeIn, extractedLibIn)) { - throw new RuntimeException(String.format( - "Failed to write a native library file at %s", - extractedLibFile)); + throw new RuntimeException( + String.format("Failed to write a native library file at %s", extractedLibFile)); } } finally { if (nativeIn != null) { @@ -209,7 +200,7 @@ final class NativeCodeLoader { } catch (final IOException e) { return null; } finally { - IoUtils.cleanup(reader); + IoUtils.closeQuietly(inputStream); } } 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 6e19f75..354b96a 100644 --- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java +++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java @@ -128,6 +128,7 @@ public class CryptoCipherFactory { CipherProvider.OPENSSL.getClassName() .concat(",") .concat(CipherProvider.JCE.getClassName()); + /** * The private Constructor of {@link CryptoCipherFactory}. */ @@ -137,16 +138,16 @@ public class CryptoCipherFactory { /** * Gets a cipher instance for specified algorithm/mode/padding. * - * @param props the configuration properties - uses {@link #CLASSES_KEY} + * @param properties the configuration properties - uses {@link #CLASSES_KEY} * @param transformation algorithm/mode/padding * @return CryptoCipher the cipher (defaults to OpenSslCipher) * @throws GeneralSecurityException if cipher initialize failed * @throws IllegalArgumentException if no classname(s) were provided */ - public static CryptoCipher getCryptoCipher(final String transformation, - final Properties props) throws GeneralSecurityException { + public static CryptoCipher getCryptoCipher(final String transformation, final Properties properties) + throws GeneralSecurityException { - final List<String> names = Utils.splitClassNames(getCipherClassString(props), ","); + final List<String> names = Utils.splitClassNames(getCipherClassString(properties), ","); if (names.size() == 0) { throw new IllegalArgumentException("No classname(s) provided"); } @@ -158,7 +159,7 @@ public class CryptoCipherFactory { try { final Class<?> cls = ReflectionUtils.getClassByName(klass); cipher = ReflectionUtils.newInstance(cls.asSubclass - (CryptoCipher.class), props, transformation); + (CryptoCipher.class), properties, transformation); if (cipher != null) { break; } diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java index 483cb51..c049388 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java @@ -47,9 +47,10 @@ import com.sun.jna.ptr.PointerByReference; * http://en.wikipedia.org/wiki/RdRand</a> */ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom { + private static final long serialVersionUID = -7128193502768749585L; private final boolean rdrandEnabled; - private PointerByReference rdrandEngine; + private transient PointerByReference rdrandEngine; /** * Constructs a {@link OpenSslJnaCryptoRandom}. diff --git a/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java b/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java index 05ab89c..cda6653 100644 --- a/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java +++ b/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java @@ -27,6 +27,11 @@ import java.util.Random; */ class JavaCryptoRandom extends Random implements CryptoRandom { + /** + * Generated serialVersionUID. + */ + private static final long serialVersionUID = 5517475898166660050L; + private SecureRandom instance; /** 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 722df6b..7490180 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java @@ -109,19 +109,19 @@ public class CryptoInputStream extends InputStream implements * <i>AES/CBC/PKCS5Padding</i>. * See the Java Cryptography Architecture Standard Algorithm Name Documentation * for information about standard transformation names. - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param in the input stream. + * @param inputStream the input stream. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CryptoInputStream. public CryptoInputStream(final String transformation, - final Properties props, final InputStream in, final Key key, + final Properties properties, final InputStream inputStream, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(in, Utils.getCipherInstance(transformation, props), - CryptoInputStream.getBufferSize(props), key, params); + this(inputStream, Utils.getCipherInstance(transformation, properties), + CryptoInputStream.getBufferSize(properties), key, params); } /** @@ -131,51 +131,51 @@ public class CryptoInputStream extends InputStream implements * <i>AES/CBC/PKCS5Padding</i>. * See the Java Cryptography Architecture Standard Algorithm Name Documentation * for information about standard transformation names. - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param in the ReadableByteChannel object. + * @param channel the ReadableByteChannel object. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CryptoInputStream. public CryptoInputStream(final String transformation, - final Properties props, final ReadableByteChannel in, final Key key, + final Properties properties, final ReadableByteChannel channel, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(in, Utils.getCipherInstance(transformation, props), CryptoInputStream - .getBufferSize(props), key, params); + this(channel, Utils.getCipherInstance(transformation, properties), CryptoInputStream + .getBufferSize(properties), key, params); } /** * Constructs a {@link CryptoInputStream}. * * @param cipher the cipher instance. - * @param in the input stream. + * @param inputStream the input stream. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ - protected CryptoInputStream(final InputStream in, final CryptoCipher cipher, + protected CryptoInputStream(final InputStream inputStream, final CryptoCipher cipher, final int bufferSize, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(new StreamInput(in, bufferSize), cipher, bufferSize, key, params); + this(new StreamInput(inputStream, bufferSize), cipher, bufferSize, key, params); } /** * Constructs a {@link CryptoInputStream}. * - * @param in the ReadableByteChannel instance. + * @param channel the ReadableByteChannel instance. * @param cipher the cipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ - protected CryptoInputStream(final ReadableByteChannel in, final CryptoCipher cipher, + protected CryptoInputStream(final ReadableByteChannel channel, final CryptoCipher cipher, final int bufferSize, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(new ChannelInput(in), cipher, bufferSize, key, params); + this(new ChannelInput(channel), cipher, bufferSize, key, params); } /** diff --git a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java index 94bcd53..b581356 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java @@ -92,19 +92,19 @@ public class CryptoOutputStream extends OutputStream implements * <i>AES/CBC/PKCS5Padding</i>. * See the Java Cryptography Architecture Standard Algorithm Name Documentation * for information about standard transformation names. - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param out the output stream. + * @param outputStream the output stream. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CryptoOutputStream. public CryptoOutputStream(final String transformation, - final Properties props, final OutputStream out, final Key key, + final Properties properties, final OutputStream outputStream, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(out, Utils.getCipherInstance(transformation, props), - CryptoInputStream.getBufferSize(props), key, params); + this(outputStream, Utils.getCipherInstance(transformation, properties), + CryptoInputStream.getBufferSize(properties), key, params); } @@ -115,7 +115,7 @@ public class CryptoOutputStream extends OutputStream implements * <i>AES/CBC/PKCS5Padding</i>. * See the Java Cryptography Architecture Standard Algorithm Name Documentation * for information about standard transformation names. - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. * @param out the WritableByteChannel instance. * @param key crypto key for the cipher. @@ -124,27 +124,27 @@ public class CryptoOutputStream extends OutputStream implements */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CryptoOutputStream. public CryptoOutputStream(final String transformation, - final Properties props, final WritableByteChannel out, final Key key, + final Properties properties, final WritableByteChannel out, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(out, Utils.getCipherInstance(transformation, props), CryptoInputStream - .getBufferSize(props), key, params); + this(out, Utils.getCipherInstance(transformation, properties), CryptoInputStream + .getBufferSize(properties), key, params); } /** * Constructs a {@link CryptoOutputStream}. * - * @param out the output stream. + * @param outputStream the output stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ - protected CryptoOutputStream(final OutputStream out, final CryptoCipher cipher, + protected CryptoOutputStream(final OutputStream outputStream, final CryptoCipher cipher, final int bufferSize, final Key key, final AlgorithmParameterSpec params) throws IOException { - this(new StreamOutput(out, bufferSize), cipher, bufferSize, key, params); + this(new StreamOutput(outputStream, bufferSize), cipher, bufferSize, key, params); } /** 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 c23547f..db7d914 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CtrCryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CtrCryptoInputStream.java @@ -84,61 +84,61 @@ public class CtrCryptoInputStream extends CryptoInputStream { /** * Constructs a {@link CtrCryptoInputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param in the input stream. + * @param inputStream the input stream. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @throws IOException if an I/O error occurs. */ - public CtrCryptoInputStream(final Properties props, final InputStream in, final byte[] key, + public CtrCryptoInputStream(final Properties properties, final InputStream inputStream, final byte[] key, final byte[] iv) throws IOException { - this(props, in, key, iv, 0); + this(properties, inputStream, key, iv, 0); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param in the ReadableByteChannel instance. + * @param channel the ReadableByteChannel instance. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @throws IOException if an I/O error occurs. */ - public CtrCryptoInputStream(final Properties props, final ReadableByteChannel in, + public CtrCryptoInputStream(final Properties properties, final ReadableByteChannel channel, final byte[] key, final byte[] iv) throws IOException { - this(props, in, key, iv, 0); + this(properties, channel, key, iv, 0); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param in the input stream. + * @param inputStream the input stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @throws IOException if an I/O error occurs. */ - protected CtrCryptoInputStream(final InputStream in, final CryptoCipher cipher, + protected CtrCryptoInputStream(final InputStream inputStream, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv) throws IOException { - this(in, cipher, bufferSize, key, iv, 0); + this(inputStream, cipher, bufferSize, key, iv, 0); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param in the ReadableByteChannel instance. + * @param channel the ReadableByteChannel instance. * @param cipher the cipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @throws IOException if an I/O error occurs. */ - protected CtrCryptoInputStream(final ReadableByteChannel in, final CryptoCipher cipher, + protected CtrCryptoInputStream(final ReadableByteChannel channel, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv) throws IOException { - this(in, cipher, bufferSize, key, iv, 0); + this(channel, cipher, bufferSize, key, iv, 0); } /** @@ -159,26 +159,26 @@ public class CtrCryptoInputStream extends CryptoInputStream { /** * Constructs a {@link CtrCryptoInputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param in the InputStream instance. + * @param inputStream the InputStream instance. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the stream. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CtrCryptoInputStream. - public CtrCryptoInputStream(final Properties props, final InputStream in, final byte[] key, + public CtrCryptoInputStream(final Properties properties, final InputStream inputStream, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(in, Utils.getCipherInstance( - "AES/CTR/NoPadding", props), - CryptoInputStream.getBufferSize(props), key, iv, streamOffset); + this(inputStream, Utils.getCipherInstance( + "AES/CTR/NoPadding", properties), + CryptoInputStream.getBufferSize(properties), key, iv, streamOffset); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. * @param in the ReadableByteChannel instance. * @param key crypto key for the cipher. @@ -187,17 +187,17 @@ public class CtrCryptoInputStream extends CryptoInputStream { * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CtrCryptoInputStream. - public CtrCryptoInputStream(final Properties props, final ReadableByteChannel in, + 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", props), - CryptoInputStream.getBufferSize(props), key, iv, streamOffset); + "AES/CTR/NoPadding", properties), + CryptoInputStream.getBufferSize(properties), key, iv, streamOffset); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param in the InputStream instance. + * @param inputStream the InputStream instance. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. @@ -205,17 +205,17 @@ public class CtrCryptoInputStream extends CryptoInputStream { * @param streamOffset the start offset in the stream. * @throws IOException if an I/O error occurs. */ - protected CtrCryptoInputStream(final InputStream in, final CryptoCipher cipher, + protected CtrCryptoInputStream(final InputStream inputStream, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(new StreamInput(in, bufferSize), cipher, bufferSize, key, iv, + this(new StreamInput(inputStream, bufferSize), cipher, bufferSize, key, iv, streamOffset); } /** * Constructs a {@link CtrCryptoInputStream}. * - * @param in the ReadableByteChannel instance. + * @param channel the ReadableByteChannel instance. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. @@ -223,10 +223,10 @@ public class CtrCryptoInputStream extends CryptoInputStream { * @param streamOffset the start offset in the stream. * @throws IOException if an I/O error occurs. */ - protected CtrCryptoInputStream(final ReadableByteChannel in, final CryptoCipher cipher, + protected CtrCryptoInputStream(final ReadableByteChannel channel, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(new ChannelInput(in), cipher, bufferSize, key, iv, streamOffset); + this(new ChannelInput(channel), cipher, bufferSize, key, iv, streamOffset); } /** 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 f8b8ff3..7f8fc98 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CtrCryptoOutputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CtrCryptoOutputStream.java @@ -164,45 +164,45 @@ public class CtrCryptoOutputStream extends CryptoOutputStream { /** * Constructs a {@link CtrCryptoOutputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param out the output stream. + * @param outputStream the output stream. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the data. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CtrCryptoOutputStream. - public CtrCryptoOutputStream(final Properties props, final OutputStream out, + public CtrCryptoOutputStream(final Properties properties, final OutputStream outputStream, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(out, Utils.getCipherInstance( - "AES/CTR/NoPadding", props), - CryptoInputStream.getBufferSize(props), key, iv, streamOffset); + this(outputStream, Utils.getCipherInstance( + "AES/CTR/NoPadding", properties), + CryptoInputStream.getBufferSize(properties), key, iv, streamOffset); } /** * Constructs a {@link CtrCryptoOutputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. - * @param out the WritableByteChannel instance. + * @param channel the WritableByteChannel instance. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the data. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CtrCryptoOutputStream. - public CtrCryptoOutputStream(final Properties props, final WritableByteChannel out, + public CtrCryptoOutputStream(final Properties properties, final WritableByteChannel channel, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(out, Utils.getCipherInstance( - "AES/CTR/NoPadding", props), - CryptoInputStream.getBufferSize(props), key, iv, streamOffset); + this(channel, Utils.getCipherInstance( + "AES/CTR/NoPadding", properties), + CryptoInputStream.getBufferSize(properties), key, iv, streamOffset); } /** * Constructs a {@link CtrCryptoOutputStream}. * - * @param out the output stream. + * @param outputStream the output stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. @@ -210,10 +210,10 @@ public class CtrCryptoOutputStream extends CryptoOutputStream { * @param streamOffset the start offset in the data. * @throws IOException if an I/O error occurs. */ - protected CtrCryptoOutputStream(final OutputStream out, final CryptoCipher cipher, + protected CtrCryptoOutputStream(final OutputStream outputStream, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(new StreamOutput(out, bufferSize), cipher, bufferSize, key, iv, + this(new StreamOutput(outputStream, bufferSize), cipher, bufferSize, key, iv, streamOffset); } 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 4b270cd..284cad2 100644 --- a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java @@ -58,12 +58,12 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream { /** * properties for constructing a CryptoCipher */ - private final Properties props; + private final Properties properties; /** * Constructs a {@link PositionedCryptoInputStream}. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. * @param in the input data. * @param key crypto key for the cipher. @@ -72,16 +72,16 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream { * @throws IOException if an I/O error occurs. */ @SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by PositionedCryptoInputStream. - public PositionedCryptoInputStream(final Properties props, final Input in, final byte[] key, + public PositionedCryptoInputStream(final Properties properties, final Input in, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { - this(props, in, Utils.getCipherInstance("AES/CTR/NoPadding", props), - CryptoInputStream.getBufferSize(props), key, iv, streamOffset); + this(properties, in, Utils.getCipherInstance("AES/CTR/NoPadding", properties), + CryptoInputStream.getBufferSize(properties), key, iv, streamOffset); } /** * Constructs a {@link PositionedCryptoInputStream}. * - * @param props the props of stream + * @param properties the properties of stream * @param input the input data. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. @@ -90,11 +90,11 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream { * @param streamOffset the start offset in the data. * @throws IOException if an I/O error occurs. */ - protected PositionedCryptoInputStream(final Properties props, final Input input, final CryptoCipher cipher, + protected PositionedCryptoInputStream(final Properties properties, final Input input, final CryptoCipher cipher, final int bufferSize, final byte[] key, final byte[] iv, final long streamOffset) throws IOException { super(input, cipher, bufferSize, key, iv, streamOffset); - this.props = props; + this.properties = properties; } /** @@ -321,7 +321,7 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream { if (state == null) { CryptoCipher cryptoCipher; try { - cryptoCipher = CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", props); + cryptoCipher = CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", properties); } catch (final GeneralSecurityException e) { throw new IOException(e); } @@ -392,7 +392,8 @@ public class PositionedCryptoInputStream extends CtrCryptoInputStream { } } - private class CipherState { + private static class CipherState { + private final CryptoCipher cryptoCipher; private boolean reset; 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 0345b3d..f362228 100644 --- a/src/main/java/org/apache/commons/crypto/utils/Utils.java +++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java @@ -113,7 +113,7 @@ public final class Utils { * Helper method to create a CryptoCipher instance and throws only * IOException. * - * @param props The {@code Properties} class represents a set of + * @param properties The {@code Properties} class represents a set of * properties. * @param transformation the name of the transformation, e.g., * <i>AES/CBC/PKCS5Padding</i>. @@ -123,10 +123,10 @@ public final class Utils { * @throws IOException if an I/O error occurs. */ public static CryptoCipher getCipherInstance( - final String transformation, final Properties props) + final String transformation, final Properties properties) throws IOException { try { - return CryptoCipherFactory.getCryptoCipher(transformation, props); + return CryptoCipherFactory.getCryptoCipher(transformation, properties); } catch (final GeneralSecurityException e) { throw new IOException(e); } 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 ca27546..734c4c0 100644 --- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java +++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java @@ -33,7 +33,6 @@ import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import org.apache.commons.crypto.utils.ReflectionUtils; -import org.apache.commons.crypto.utils.Utils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -47,10 +46,10 @@ public abstract class AbstractCipherTest { // data public static final int BYTEBUFFER_SIZE = 1000; - public String[] cipherTests = null; - private Properties props = null; - protected String cipherClass = null; - protected String[] transformations = null; + public String[] cipherTests; + private Properties props; + protected String cipherClass; + protected String[] transformations; // cipher static final byte[] KEY = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, @@ -63,8 +62,8 @@ public abstract class AbstractCipherTest { @Before public void setup() { init(); - Utils.checkNotNull(cipherClass); - Utils.checkNotNull(transformations); + assertNotNull("cipherClass", cipherClass); + assertNotNull("transformations", transformations); props = new Properties(); props.setProperty(CryptoCipherFactory.CLASSES_KEY, cipherClass); @@ -153,19 +152,18 @@ public abstract class AbstractCipherTest { @Test(expected = RuntimeException.class) public void testNullTransform() throws Exception { - getCipher(null); + getCipher(null).close(); } @Test(expected = RuntimeException.class) public void testInvalidTransform() throws Exception { - getCipher("AES/CBR/NoPadding/garbage/garbage"); + getCipher("AES/CBR/NoPadding/garbage/garbage").close(); } @Test public void testInvalidKey() throws Exception { for (final String transform : transformations) { - try { - final CryptoCipher cipher = getCipher(transform); + try (final CryptoCipher cipher = getCipher(transform)) { Assert.assertNotNull(cipher); final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, @@ -180,8 +178,7 @@ public abstract class AbstractCipherTest { @Test public void testInvalidIV() throws Exception { for (final String transform : transformations) { - try { - final CryptoCipher cipher = getCipher(transform); + try (final CryptoCipher cipher = getCipher(transform)) { Assert.assertNotNull(cipher); final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, @@ -196,8 +193,7 @@ public abstract class AbstractCipherTest { @Test public void testInvalidIVClass() throws Exception { for (final String transform : transformations) { - try { - final CryptoCipher cipher = getCipher(transform); + try (final CryptoCipher cipher = getCipher(transform)) { Assert.assertNotNull(cipher); cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new GCMParameterSpec(IV.length, IV)); @@ -209,46 +205,43 @@ public abstract class AbstractCipherTest { private void byteBufferTest(final String transformation, final byte[] key, final byte[] iv, final ByteBuffer input, final ByteBuffer output) - throws Exception { + throws Exception { final ByteBuffer decResult = ByteBuffer.allocateDirect(BYTEBUFFER_SIZE); final ByteBuffer encResult = ByteBuffer.allocateDirect(BYTEBUFFER_SIZE); - final CryptoCipher enc = getCipher(transformation); - enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), - new IvParameterSpec(iv)); - - final CryptoCipher dec = getCipher(transformation); - dec.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), - new IvParameterSpec(iv)); - - // - // encryption pass - // - enc.doFinal(input, encResult); - input.flip(); - encResult.flip(); - if (!output.equals(encResult)) { - final byte[] b = new byte[output.remaining()]; - output.get(b); - final byte[] c = new byte[encResult.remaining()]; - encResult.get(c); - Assert.fail("AES failed encryption - expected " - + new String(DatatypeConverter.printHexBinary(b)) + " got " - + new String(DatatypeConverter.printHexBinary(c))); - } + 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)); + + // + // encryption pass + // + enc.doFinal(input, encResult); + input.flip(); + encResult.flip(); + if (!output.equals(encResult)) { + final byte[] b = new byte[output.remaining()]; + output.get(b); + final byte[] c = new byte[encResult.remaining()]; + encResult.get(c); + Assert.fail("AES failed encryption - expected " + new String(DatatypeConverter.printHexBinary(b)) + + " got " + new String(DatatypeConverter.printHexBinary(c))); + } - // - // decryption pass - // - dec.doFinal(encResult, decResult); - decResult.flip(); - - if (!input.equals(decResult)) { - final byte[] inArray = new byte[input.remaining()]; - final byte[] decResultArray = new byte[decResult.remaining()]; - input.get(inArray); - decResult.get(decResultArray); - Assert.fail(); + // + // decryption pass + // + dec.doFinal(encResult, decResult); + decResult.flip(); + + if (!input.equals(decResult)) { + final byte[] inArray = new byte[input.remaining()]; + final byte[] decResultArray = new byte[decResult.remaining()]; + input.get(inArray); + decResult.get(decResultArray); + Assert.fail(); + } } } 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 f71c6df..f84a3d8 100644 --- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java +++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java @@ -64,7 +64,7 @@ public abstract class AbstractCipherStreamTest { public abstract void setUp() throws IOException; @Before - public void before() throws IOException { + public void before() throws Exception { final Random random = new SecureRandom(); random.nextBytes(data); random.nextBytes(key); @@ -131,7 +131,8 @@ public abstract class AbstractCipherStreamTest { return; // Skip this test if no JNI } } - try (InputStream in = getCryptoInputStream( + try (@SuppressWarnings("resource") // The CryptoCipher returned by getCipherInstance() is closed by CryptoInputStream. + InputStream in = newCryptoInputStream( new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { final byte[] result = new byte[dataLen]; @@ -171,139 +172,139 @@ public abstract class AbstractCipherStreamTest { } ByteBuffer buf = ByteBuffer.allocate(dataLen + 100); // Default buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { byteBufferReadCheck(in, buf, 0); } // Default buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Small buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), smallBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 0); } // Small buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), smallBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, default buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { buf = ByteBuffer.allocateDirect(dataLen + 100); byteBufferReadCheck(in, buf, 0); } // Direct buffer, default buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, small buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), smallBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 0); } // Direct buffer, small buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), smallBufferSize, iv, withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, small buffer size, initial buffer position is 0, final read - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), smallBufferSize, iv, withChannel)) { buf.clear(); byteBufferFinalReadCheck(in, buf, 0); } // Default buffer size, initial buffer position is 0, insufficient dest buffer length - try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + try (InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel)) { buf = ByteBuffer.allocate(100); byteBufferReadCheck(in, buf, 0); } // Default buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf = ByteBuffer.allocate(dataLen + 100); byteBufferReadCheck(in, buf, 0); } // Default buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Small buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 0); } // Small buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, default buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf = ByteBuffer.allocateDirect(dataLen + 100); byteBufferReadCheck(in, buf, 0); } // Direct buffer, default buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, small buffer size, initial buffer position is 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 0); } // Direct buffer, small buffer size, initial buffer position is not 0 - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferReadCheck(in, buf, 11); } // Direct buffer, default buffer size, initial buffer position is 0, final read - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf.clear(); byteBufferFinalReadCheck(in, buf, 0); } // Default buffer size, initial buffer position is 0, insufficient dest buffer length - try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + try (InputStream in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, new IvParameterSpec(iv), withChannel)) { buf = ByteBuffer.allocate(100); byteBufferReadCheck(in, buf, 0); @@ -319,20 +320,20 @@ public abstract class AbstractCipherStreamTest { } } baos.reset(); - CryptoOutputStream out = getCryptoOutputStream(baos, + CryptoOutputStream out = newCryptoOutputStream(baos, getCipher(cipherClass), defaultBufferSize, iv, withChannel); doByteBufferWrite(out, withChannel); baos.reset(); final CryptoCipher cipher = getCipher(cipherClass); final String transformation = cipher.getAlgorithm(); - out = getCryptoOutputStream(transformation, props, baos, key, + out = newCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), withChannel); doByteBufferWrite(out, withChannel); out.write(1); Assert.assertTrue(out.isOpen()); - out = getCryptoOutputStream(transformation, props, baos, key, + out = newCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), withChannel); out.close(); Assert.assertTrue(!out.isOpen()); @@ -351,9 +352,8 @@ public abstract class AbstractCipherStreamTest { // Test InvalidAlgorithmParameters try { - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), - new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), - withChannel); + in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), + new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), withChannel); Assert.fail("Expected IOException."); } catch (final IOException ex) { Assert.assertEquals(ex.getMessage(),"Illegal parameters"); @@ -361,7 +361,7 @@ public abstract class AbstractCipherStreamTest { // Test InvalidAlgorithmParameters try { - out = getCryptoOutputStream(transformation, props, baos, + out = newCryptoOutputStream(transformation, props, baos, new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), withChannel); Assert.fail("Expected IOException."); @@ -371,7 +371,7 @@ public abstract class AbstractCipherStreamTest { // Test Invalid Key try { - in = getCryptoInputStream(transformation,props, new ByteArrayInputStream(encData), + in = newCryptoInputStream(transformation,props, new ByteArrayInputStream(encData), new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel); Assert.fail("Expected IOException for Invalid Key"); } catch (final IOException ex) { @@ -380,7 +380,7 @@ public abstract class AbstractCipherStreamTest { // Test Invalid Key try { - out = getCryptoOutputStream(transformation, props, baos, new byte[10], + out = newCryptoOutputStream(transformation, props, baos, new byte[10], new IvParameterSpec(iv), withChannel); Assert.fail("Expected IOException for Invalid Key"); } catch (final IOException ex) { @@ -389,7 +389,7 @@ public abstract class AbstractCipherStreamTest { // Test reading a closed stream. try { - in = getCryptoInputStream(new ByteArrayInputStream(encData), + in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel); in.close(); in.read(); // Throw exception. @@ -406,7 +406,7 @@ public abstract class AbstractCipherStreamTest { // Test checking a closed stream. try { - out = getCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), + out = newCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), withChannel); out.close(); ((CryptoOutputStream)out).checkStream(); // Throw exception. @@ -432,21 +432,21 @@ public abstract class AbstractCipherStreamTest { // Test unsupported operation handling. try { - in = getCryptoInputStream(new ByteArrayInputStream(encData), + in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, false); in.mark(0); assertEquals(false, in.markSupported()); in.reset(); Assert.fail("Expected IOException."); } catch (final IOException ex) { - Assert.assertTrue(ex.getMessage().equals("Mark/reset not supported")); + Assert.assertTrue(ex.getMessage().equals("mark/reset not supported")); } finally { in.close(); } } protected void doFieldGetterTest(final String cipherClass, final ByteArrayOutputStream baos, - final boolean withChannel) throws Exception { + final boolean withChannel) throws Exception { if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) { if (!Crypto.isNativeCodeLoaded()) { return; // Skip this test if no JNI @@ -455,7 +455,7 @@ public abstract class AbstractCipherStreamTest { final CryptoCipher cipher = getCipher(cipherClass); - final CryptoInputStream in = getCryptoInputStream( + final CryptoInputStream in = newCryptoInputStream( new ByteArrayInputStream(encData), cipher, defaultBufferSize, iv, withChannel); @@ -470,7 +470,7 @@ public abstract class AbstractCipherStreamTest { Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class); Assert.assertNotNull(in.getInput()); - final CryptoOutputStream out = getCryptoOutputStream(baos, getCipher(cipherClass), + final CryptoOutputStream out = newCryptoOutputStream(baos, getCipher(cipherClass), defaultBufferSize, iv, withChannel); Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + cipher.getBlockSize()); @@ -565,7 +565,7 @@ public abstract class AbstractCipherStreamTest { out.flush(); - try (InputStream in = getCryptoInputStream( + try (InputStream in = newCryptoInputStream( new ByteArrayInputStream(encData), out.getCipher(), defaultBufferSize, iv, withChannel)) { buf = ByteBuffer.allocate(dataLen + 100); @@ -573,7 +573,7 @@ public abstract class AbstractCipherStreamTest { } } - protected CryptoInputStream getCryptoInputStream(final ByteArrayInputStream bais, + protected CryptoInputStream newCryptoInputStream(final ByteArrayInputStream bais, final CryptoCipher cipher, final int bufferSize, final byte[] iv, final boolean withChannel) throws IOException { if (withChannel) { @@ -585,7 +585,7 @@ public abstract class AbstractCipherStreamTest { new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); } - protected CryptoInputStream getCryptoInputStream(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) { @@ -594,7 +594,7 @@ public abstract class AbstractCipherStreamTest { return new CryptoInputStream(transformation, props, bais, new SecretKeySpec(key, "AES"), params); } - protected CryptoInputStream getCryptoInputStream(final String transformation, + protected CryptoInputStream newCryptoInputStream(final String transformation, final Properties props, final ByteArrayInputStream bais, final Key key, final AlgorithmParameterSpec params, final boolean withChannel) throws IOException { if (withChannel) { @@ -603,7 +603,7 @@ public abstract class AbstractCipherStreamTest { return new CryptoInputStream(transformation, props, bais, key, params); } - protected CryptoOutputStream getCryptoOutputStream( + protected CryptoOutputStream newCryptoOutputStream( final ByteArrayOutputStream baos, final CryptoCipher cipher, final int bufferSize, final byte[] iv, final boolean withChannel) throws IOException { if (withChannel) { @@ -615,7 +615,7 @@ public abstract class AbstractCipherStreamTest { new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); } - protected CryptoOutputStream getCryptoOutputStream(final String transformation, + protected CryptoOutputStream newCryptoOutputStream(final String transformation, final Properties props, final ByteArrayOutputStream baos, final byte[] key, final AlgorithmParameterSpec param, final boolean withChannel) throws IOException { if (withChannel) { @@ -626,7 +626,7 @@ public abstract class AbstractCipherStreamTest { param); } - protected CryptoOutputStream getCryptoOutputStream(final String transformation, + protected CryptoOutputStream newCryptoOutputStream(final String transformation, final Properties props, final ByteArrayOutputStream baos, final Key key, final AlgorithmParameterSpec params, final boolean withChannel) throws IOException { if (withChannel) { @@ -706,7 +706,7 @@ public abstract class AbstractCipherStreamTest { // Encrypt data final ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); - try (CryptoOutputStream out = getCryptoOutputStream(encryptedData, + try (CryptoOutputStream out = newCryptoOutputStream(encryptedData, encCipher, defaultBufferSize, iv, false)) { out.write(originalData, 0, originalData.length); out.flush(); @@ -716,7 +716,7 @@ public abstract class AbstractCipherStreamTest { final CryptoCipher decCipher = getCipher(decCipherClass); // Decrypt data - CryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream( + CryptoInputStream in = newCryptoInputStream(new ByteArrayInputStream( encryptedData.toByteArray()), decCipher, defaultBufferSize, iv, false); @@ -736,7 +736,7 @@ public abstract class AbstractCipherStreamTest { originalData, decryptedData); // Decrypt data byte-at-a-time - in = getCryptoInputStream( + in = newCryptoInputStream( new ByteArrayInputStream(encryptedData.toByteArray()), decCipher, defaultBufferSize, iv, false); @@ -774,7 +774,7 @@ public abstract class AbstractCipherStreamTest { // Encrypt data final ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); - try (CryptoOutputStream out = getCryptoOutputStream(encryptedData, + try (CryptoOutputStream out = newCryptoOutputStream(encryptedData, encCipher, defaultBufferSize, iv, true)) { out.write(originalData, 0, originalData.length); out.flush(); @@ -784,7 +784,7 @@ public abstract class AbstractCipherStreamTest { final CryptoCipher decCipher = getCipher(decCipherClass); // Decrypt data - CryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream( + CryptoInputStream in = newCryptoInputStream(new ByteArrayInputStream( encryptedData.toByteArray()), decCipher, defaultBufferSize, iv, true); @@ -804,7 +804,7 @@ public abstract class AbstractCipherStreamTest { originalData, decryptedData); // Decrypt data byte-at-a-time - in = getCryptoInputStream( + in = newCryptoInputStream( new ByteArrayInputStream(encryptedData.toByteArray()), decCipher, defaultBufferSize, iv, true); 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 d37a5d4..fa9416d 100644 --- a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java +++ b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java @@ -44,7 +44,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest { } @Override - protected CtrCryptoInputStream getCryptoInputStream( + protected CtrCryptoInputStream newCryptoInputStream( final ByteArrayInputStream bais, final CryptoCipher cipher, final int bufferSize, final byte[] iv, final boolean withChannel) throws IOException { if (withChannel) { @@ -55,7 +55,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest { } @Override - protected CtrCryptoInputStream getCryptoInputStream(final String transformation, final Properties props, + protected CtrCryptoInputStream newCryptoInputStream(final String transformation, final Properties props, final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params, final boolean withChannel) throws IOException { if (withChannel) { @@ -66,7 +66,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest { } @Override - protected CtrCryptoOutputStream getCryptoOutputStream( + protected CtrCryptoOutputStream newCryptoOutputStream( final ByteArrayOutputStream baos, final CryptoCipher cipher, final int bufferSize, final byte[] iv, final boolean withChannel) throws IOException { if (withChannel) { @@ -77,7 +77,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest { } @Override - protected CtrCryptoOutputStream getCryptoOutputStream(final String transformation, + protected CtrCryptoOutputStream newCryptoOutputStream(final String transformation, final Properties props, final ByteArrayOutputStream baos, final byte[] key, final AlgorithmParameterSpec params, final boolean withChannel) throws IOException { if (withChannel) { @@ -165,7 +165,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest { protected void doDecryptTest(final String cipherClass, final boolean withChannel) throws IOException { - final CtrCryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), + final CtrCryptoInputStream in = newCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), defaultBufferSize, iv, withChannel); final ByteBuffer buf = ByteBuffer.allocateDirect(dataLen);