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 2020/06/06 23:32:56 UTC

[commons-crypto] branch master updated (118ba87 -> 4cfe171)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git.


    from 118ba87  Update some info for Java version and other Windows apt help.
     new 99cbed5  No need to nest else.
     new 0e7084d  Fix Javadoc.
     new b2eb1fc  Remove unused imports.
     new 9860800  Use try-with-resources.
     new f6b718a  Remove trailing white space.
     new 521aac8  Use final.
     new 4cfe171  Add missing annotations.

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../crypto/cipher/OpenSslGaloisCounterMode.java    |  16 +-
 .../commons/crypto/jna/OpenSslJnaCipher.java       |   4 +-
 .../commons/crypto/jna/OpenSslNativeJna.java       |  70 ++--
 .../commons/crypto/random/JavaCryptoRandom.java    |   4 +-
 .../commons/crypto/cipher/AbstractCipherTest.java  |  10 +-
 .../commons/crypto/cipher/OpenSslCipherTest.java   |   3 +
 .../commons/crypto/jna/OpenSslJnaCipherTest.java   |   1 -
 .../crypto/stream/AbstractCipherStreamTest.java    | 377 ++++++++++-----------
 .../commons/crypto/stream/CtrCryptoStreamTest.java |  78 ++---
 .../stream/PositionedCryptoInputStreamTest.java    |  20 +-
 10 files changed, 278 insertions(+), 305 deletions(-)


[commons-crypto] 01/07: No need to nest else.

Posted by gg...@apache.org.
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

commit 99cbed5a8f9192a32dab07d49e01daf082bed69a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 18:18:53 2020 -0400

    No need to nest else.
---
 .../crypto/cipher/OpenSslGaloisCounterMode.java    | 16 +++--
 .../commons/crypto/jna/OpenSslNativeJna.java       | 70 ++++++++--------------
 2 files changed, 31 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/cipher/OpenSslGaloisCounterMode.java b/src/main/java/org/apache/commons/crypto/cipher/OpenSslGaloisCounterMode.java
index 29682c8..efbae23 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/OpenSslGaloisCounterMode.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/OpenSslGaloisCounterMode.java
@@ -95,13 +95,12 @@ class OpenSslGaloisCounterMode extends OpenSslFeedbackCipher {
             input.get(inputBuf, 0, inputLen);
             inBuffer.write(inputBuf, 0, inputLen);
             return 0;
-        } else {
-            len = OpenSslNative.update(context, input, input.position(),
-                    input.remaining(), output, output.position(),
-                    output.remaining());
-            input.position(input.limit());
-            output.position(output.position() + len);
         }
+        len = OpenSslNative.update(context, input, input.position(),
+                input.remaining(), output, output.position(),
+                output.remaining());
+        input.position(input.limit());
+        output.position(output.position() + len);
 
         return len;
     }
@@ -119,10 +118,9 @@ class OpenSslGaloisCounterMode extends OpenSslFeedbackCipher {
             // is successfully verified
             inBuffer.write(input, inputOffset, inputLen);
             return 0;
-        } else {
-            return OpenSslNative.updateByteArray(context, input, inputOffset,
-                    inputLen, output, outputOffset, output.length - outputOffset);
         }
+        return OpenSslNative.updateByteArray(context, input, inputOffset,
+                inputLen, output, outputOffset, output.length - outputOffset);
     }
 
     @Override
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
index 4680580..d5bc738 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
@@ -73,9 +73,8 @@ class OpenSslNativeJna {
     public static PointerByReference ENGINE_by_id(final String string) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.ENGINE_by_id(string);
-        } else {
-            return OpenSsl10XNativeJna.ENGINE_by_id(string);
         }
+        return OpenSsl10XNativeJna.ENGINE_by_id(string);
     }
 
     public static void ENGINE_finish(final PointerByReference rdrandEngine) {
@@ -97,81 +96,71 @@ class OpenSslNativeJna {
     public static int ENGINE_init(final PointerByReference rdrandEngine) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.ENGINE_init(rdrandEngine);
-        } else {
-            return OpenSsl10XNativeJna.ENGINE_init(rdrandEngine);
         }
+        return OpenSsl10XNativeJna.ENGINE_init(rdrandEngine);
     }
 
     public static int ENGINE_set_default(final PointerByReference rdrandEngine, final int eNGINE_METHOD_RAND) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.ENGINE_set_default(rdrandEngine, eNGINE_METHOD_RAND);
-        } else {
-            return OpenSsl10XNativeJna.ENGINE_set_default(rdrandEngine, eNGINE_METHOD_RAND);
         }
+        return OpenSsl10XNativeJna.ENGINE_set_default(rdrandEngine, eNGINE_METHOD_RAND);
     }
 
     public static String ERR_error_string(final NativeLong err, final Object object) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.ERR_error_string(err, null);
-        } else {
-            return OpenSsl10XNativeJna.ERR_error_string(err, null);
         }
+        return OpenSsl10XNativeJna.ERR_error_string(err, null);
     }
 
     public static NativeLong ERR_peek_error() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.ERR_peek_error();
-        } else {
-            return OpenSsl10XNativeJna.ERR_peek_error();
         }
+        return OpenSsl10XNativeJna.ERR_peek_error();
     }
 
     public static PointerByReference EVP_aes_128_cbc() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_128_cbc();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_128_cbc();
         }
+        return OpenSsl10XNativeJna.EVP_aes_128_cbc();
     }
 
     public static PointerByReference EVP_aes_128_ctr() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_128_ctr();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_128_ctr();
         }
+        return OpenSsl10XNativeJna.EVP_aes_128_ctr();
     }
 
     public static PointerByReference EVP_aes_192_cbc() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_192_cbc();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_192_cbc();
         }
+        return OpenSsl10XNativeJna.EVP_aes_192_cbc();
     }
 
     public static PointerByReference EVP_aes_192_ctr() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_192_ctr();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_192_ctr();
         }
+        return OpenSsl10XNativeJna.EVP_aes_192_ctr();
     }
 
     public static PointerByReference EVP_aes_256_cbc() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_256_cbc();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_256_cbc();
         }
+        return OpenSsl10XNativeJna.EVP_aes_256_cbc();
     }
 
     public static PointerByReference EVP_aes_256_ctr() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_aes_256_ctr();
-        } else {
-            return OpenSsl10XNativeJna.EVP_aes_256_ctr();
         }
+        return OpenSsl10XNativeJna.EVP_aes_256_ctr();
     }
 
     public static void EVP_CIPHER_CTX_free(final PointerByReference context) {
@@ -185,9 +174,8 @@ class OpenSslNativeJna {
     public static PointerByReference EVP_CIPHER_CTX_new() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_CIPHER_CTX_new();
-        } else {
-            return OpenSsl10XNativeJna.EVP_CIPHER_CTX_new();
         }
+        return OpenSsl10XNativeJna.EVP_CIPHER_CTX_new();
     }
 
     public static void EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
@@ -202,9 +190,8 @@ class OpenSslNativeJna {
             final int[] outlen) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_CipherFinal_ex(context, outBuffer, outlen);
-        } else {
-            return OpenSsl10XNativeJna.EVP_CipherFinal_ex(context, outBuffer, outlen);
         }
+        return OpenSsl10XNativeJna.EVP_CipherFinal_ex(context, outBuffer, outlen);
     }
 
     public static int EVP_CipherInit_ex(final PointerByReference context, final PointerByReference algo,
@@ -212,10 +199,9 @@ class OpenSslNativeJna {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_CipherInit_ex(context, algo, null, encoded, iv,
                     cipherMode);
-        } else {
-            return OpenSsl10XNativeJna.EVP_CipherInit_ex(context, algo, null, encoded, iv,
-                    cipherMode);
         }
+        return OpenSsl10XNativeJna.EVP_CipherInit_ex(context, algo, null, encoded, iv,
+                cipherMode);
     }
 
     public static int EVP_CipherUpdate(final PointerByReference context, final ByteBuffer outBuffer,
@@ -223,65 +209,57 @@ class OpenSslNativeJna {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.EVP_CipherUpdate(context, outBuffer, outlen, inBuffer,
                     remaining);
-        } else {
-            return OpenSsl10XNativeJna.EVP_CipherUpdate(context, outBuffer, outlen, inBuffer,
-                    remaining);
         }
+        return OpenSsl10XNativeJna.EVP_CipherUpdate(context, outBuffer, outlen, inBuffer,
+                remaining);
     }
 
     public static int RAND_bytes(final ByteBuffer buf, final int length) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.RAND_bytes(buf, length);
-        } else {
-            return OpenSsl10XNativeJna.RAND_bytes(buf, length);
         }
+        return OpenSsl10XNativeJna.RAND_bytes(buf, length);
     }
 
     public static PointerByReference RAND_get_rand_method() {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.RAND_get_rand_method();
-        } else {
-            return OpenSsl10XNativeJna.RAND_get_rand_method();
         }
+        return OpenSsl10XNativeJna.RAND_get_rand_method();
     }
 
     public static PointerByReference RAND_SSLeay() {
         if (VERSION == VERSION_1_1_X) {
             return null;
-        } else {
-            return OpenSsl10XNativeJna.RAND_SSLeay();
         }
+        return OpenSsl10XNativeJna.RAND_SSLeay();
     }
 
     public static String OpenSSLVersion(final int i) {
         if (VERSION == VERSION_1_1_X) {
             return OpenSsl11XNativeJna.OpenSSL_version(i);
-        } else {
-            return OpenSsl10XNativeJna.SSLeay_version(i);
         }
+        return OpenSsl10XNativeJna.SSLeay_version(i);
     }
 
     public static void ENGINE_load_rdrand() {
         if (VERSION == VERSION_1_1_X) {
             return;
-        } else {
-            OpenSsl10XNativeJna.ENGINE_load_rdrand();
         }
+        OpenSsl10XNativeJna.ENGINE_load_rdrand();
     }
 
     public static void ENGINE_cleanup() {
         if (VERSION == VERSION_1_1_X) {
             return;
-        } else {
-            OpenSsl10XNativeJna.ENGINE_cleanup();
         }
+        OpenSsl10XNativeJna.ENGINE_cleanup();
     }
 
     public static void EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
         if (VERSION == VERSION_1_1_X) {
             return;
-        } else {
-            OpenSsl10XNativeJna.EVP_CIPHER_CTX_cleanup(context);
         }
+        OpenSsl10XNativeJna.EVP_CIPHER_CTX_cleanup(context);
     }
 }
\ No newline at end of file


[commons-crypto] 06/07: Use final.

Posted by gg...@apache.org.
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

commit 521aac8c35241601ed70886f7f59a95ab8b02ab1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:28:03 2020 -0400

    Use final.
---
 .../commons/crypto/cipher/AbstractCipherTest.java  |  6 +--
 .../crypto/stream/AbstractCipherStreamTest.java    | 52 +++++++++++-----------
 .../commons/crypto/stream/CtrCryptoStreamTest.java | 32 ++++++-------
 .../stream/PositionedCryptoInputStreamTest.java    |  8 ++--
 4 files changed, 49 insertions(+), 49 deletions(-)

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 3ae06a0..ca27546 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -163,7 +163,7 @@ public abstract class AbstractCipherTest {
 
     @Test
     public void testInvalidKey() throws Exception {
-        for (String transform : transformations) {
+        for (final String transform : transformations) {
             try {
                 final CryptoCipher cipher = getCipher(transform);
                 Assert.assertNotNull(cipher);
@@ -179,7 +179,7 @@ public abstract class AbstractCipherTest {
 
     @Test
     public void testInvalidIV() throws Exception {
-        for (String transform : transformations) {
+        for (final String transform : transformations) {
             try {
                 final CryptoCipher cipher = getCipher(transform);
                 Assert.assertNotNull(cipher);
@@ -195,7 +195,7 @@ public abstract class AbstractCipherTest {
 
     @Test
     public void testInvalidIVClass() throws Exception {
-        for (String transform : transformations) {
+        for (final String transform : transformations) {
             try {
                 final CryptoCipher cipher = getCipher(transform);
                 Assert.assertNotNull(cipher);
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 9f4203e..f71c6df 100644
--- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -324,8 +324,8 @@ public abstract class AbstractCipherStreamTest {
         doByteBufferWrite(out, withChannel);
 
         baos.reset();
-        CryptoCipher cipher = getCipher(cipherClass);
-        String transformation = cipher.getAlgorithm();
+        final CryptoCipher cipher = getCipher(cipherClass);
+        final String transformation = cipher.getAlgorithm();
         out = getCryptoOutputStream(transformation, props, baos, key,
                 new IvParameterSpec(iv), withChannel);
         doByteBufferWrite(out, withChannel);
@@ -338,7 +338,7 @@ public abstract class AbstractCipherStreamTest {
         Assert.assertTrue(!out.isOpen());
     }
 
-    protected void doExceptionTest(final String cipherClass, ByteArrayOutputStream baos,
+    protected void doExceptionTest(final String cipherClass, final ByteArrayOutputStream baos,
             final boolean withChannel) throws IOException {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
@@ -355,7 +355,7 @@ public abstract class AbstractCipherStreamTest {
                     new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]),
                     withChannel);
             Assert.fail("Expected IOException.");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertEquals(ex.getMessage(),"Illegal parameters");
         }
 
@@ -365,7 +365,7 @@ public abstract class AbstractCipherStreamTest {
                     new SecretKeySpec(key, "AES"), new GCMParameterSpec(0,
                     new byte[0]), withChannel);
             Assert.fail("Expected IOException.");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
         	Assert.assertEquals(ex.getMessage(),"Illegal parameters");
         }
 
@@ -374,7 +374,7 @@ public abstract class AbstractCipherStreamTest {
             in = getCryptoInputStream(transformation,props, new ByteArrayInputStream(encData),
                     new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel);
             Assert.fail("Expected IOException for Invalid Key");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertNotNull(ex);
         }
 
@@ -383,7 +383,7 @@ public abstract class AbstractCipherStreamTest {
             out = getCryptoOutputStream(transformation, props, baos, new byte[10],
                     new IvParameterSpec(iv), withChannel);
             Assert.fail("Expected IOException for Invalid Key");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertNotNull(ex);
         }
 
@@ -393,14 +393,14 @@ public abstract class AbstractCipherStreamTest {
                     getCipher(cipherClass), defaultBufferSize, iv, withChannel);
             in.close();
             in.read(); // Throw exception.
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("Stream closed"));
         }
 
         // Test closing a closed stream.
         try {
             in.close(); // Don't throw exception on double-close.
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.fail("Should not throw exception closing a closed stream.");
         }
 
@@ -410,21 +410,21 @@ public abstract class AbstractCipherStreamTest {
                     withChannel);
             out.close();
             ((CryptoOutputStream)out).checkStream(); // Throw exception.
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("Stream closed"));
         }
 
         // Test closing a closed stream.
         try {
             out.close(); // Don't throw exception.
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.fail("Should not throw exception closing a closed stream.");
         }
 
         // Test checkStreamCipher
         try {
             CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is required"));
         } finally {
             in.close();
@@ -438,14 +438,14 @@ public abstract class AbstractCipherStreamTest {
             assertEquals(false, in.markSupported());
             in.reset();
             Assert.fail("Expected IOException.");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("Mark/reset not supported"));
         } finally {
             in.close();
         }
     }
 
-    protected void doFieldGetterTest(final String cipherClass, ByteArrayOutputStream baos,
+    protected void doFieldGetterTest(final String cipherClass, final ByteArrayOutputStream baos,
             final boolean withChannel) throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
@@ -453,14 +453,14 @@ public abstract class AbstractCipherStreamTest {
             }
         }
 
-        CryptoCipher cipher = getCipher(cipherClass);
+        final CryptoCipher cipher = getCipher(cipherClass);
 
-        CryptoInputStream in = getCryptoInputStream(
+        final CryptoInputStream in = getCryptoInputStream(
                 new ByteArrayInputStream(encData), cipher, defaultBufferSize,
                 iv, withChannel);
 
-        Properties props = new Properties();
-        String bufferSize = Integer.toString(defaultBufferSize / 2);
+        final Properties props = new Properties();
+        final String bufferSize = Integer.toString(defaultBufferSize / 2);
         props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
 
         Assert.assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
@@ -470,7 +470,7 @@ public abstract class AbstractCipherStreamTest {
         Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
         Assert.assertNotNull(in.getInput());
 
-        CryptoOutputStream out = getCryptoOutputStream(baos, getCipher(cipherClass),
+        final CryptoOutputStream out = getCryptoOutputStream(baos, getCipher(cipherClass),
                 defaultBufferSize, iv, withChannel);
 
         Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + cipher.getBlockSize());
@@ -494,7 +494,7 @@ public abstract class AbstractCipherStreamTest {
         try {
             in.read(readData, -1, 0);
             Assert.fail("Expected IndexOutOfBoundsException.");
-        } catch (IndexOutOfBoundsException ex) {
+        } catch (final IndexOutOfBoundsException ex) {
             Assert.assertNotNull(ex);
         }
     }
@@ -509,7 +509,7 @@ public abstract class AbstractCipherStreamTest {
             len += n;
         } while (n > 0);
         buf.rewind();
-        byte[] readData = new byte[len + 1];
+        final byte[] readData = new byte[len + 1];
         buf.get(readData);
         final byte[] expectedData = new byte[len + 1];
         System.arraycopy(data, 0, expectedData, 0, len + 1);
@@ -536,7 +536,7 @@ public abstract class AbstractCipherStreamTest {
         encData = baos.toByteArray();
     }
 
-    private void doByteBufferWrite(CryptoOutputStream out, boolean withChannel) throws Exception {
+    private void doByteBufferWrite(final CryptoOutputStream out, final boolean withChannel) throws Exception {
         ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
         buf.put(data, 0, dataLen / 2);
         buf.flip();
@@ -559,7 +559,7 @@ public abstract class AbstractCipherStreamTest {
         try {
             out.write(data, 0, data.length + 1);
             Assert.fail("Expected IndexOutOfBoundsException.");
-        } catch (IndexOutOfBoundsException ex) {
+        } catch (final IndexOutOfBoundsException ex) {
             Assert.assertNotNull(ex);
         }
 
@@ -587,7 +587,7 @@ public abstract class AbstractCipherStreamTest {
 
     protected CryptoInputStream getCryptoInputStream(final String transformation, final Properties props,
     	    final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
-    	    boolean withChannel) throws IOException {
+    	    final boolean withChannel) throws IOException {
         if (withChannel) {
     	    return new CryptoInputStream(transformation, props, Channels.newChannel(bais), new SecretKeySpec(key, "AES"), params);
     	}
@@ -596,7 +596,7 @@ public abstract class AbstractCipherStreamTest {
 
     protected CryptoInputStream getCryptoInputStream(final String transformation,
             final Properties props, final ByteArrayInputStream bais, final Key key,
-            final AlgorithmParameterSpec params, boolean withChannel) throws IOException {
+            final AlgorithmParameterSpec params, final boolean withChannel) throws IOException {
         if (withChannel) {
             return new CryptoInputStream(transformation, props, Channels.newChannel(bais), key, params);
         }
@@ -628,7 +628,7 @@ public abstract class AbstractCipherStreamTest {
 
     protected CryptoOutputStream getCryptoOutputStream(final String transformation,
             final Properties props, final ByteArrayOutputStream baos, final Key key,
-            final AlgorithmParameterSpec params, boolean withChannel) throws IOException {
+            final AlgorithmParameterSpec params, final boolean withChannel) throws IOException {
         if (withChannel) {
             return new CryptoOutputStream(transformation, props, Channels.newChannel(baos), key, params);
         }
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 5d7d14e..d37a5d4 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
@@ -57,7 +57,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
     @Override
     protected CtrCryptoInputStream getCryptoInputStream(final String transformation, final Properties props,
             final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
-            boolean withChannel) throws IOException {
+            final boolean withChannel) throws IOException {
         if (withChannel) {
             return new CtrCryptoInputStream(props, Channels.newChannel(bais), key,
                     ((IvParameterSpec)params).getIV());
@@ -96,41 +96,41 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
             }
         }
 
-        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        final StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
         try {
             streamInput.seek(0);
             Assert.fail("Expected UnsupportedOperationException.");
-        } catch (UnsupportedOperationException ex) {
+        } catch (final UnsupportedOperationException ex) {
         	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
         }
         try {
             streamInput.read(0, new byte[0], 0, 0);
             Assert.fail("Expected UnsupportedOperationException.");
-        } catch (UnsupportedOperationException ex) {
+        } catch (final UnsupportedOperationException ex) {
             Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
         }
         Assert.assertEquals(streamInput.available(), encData.length);
 
-        ChannelInput channelInput = new ChannelInput(Channels.newChannel(new ByteArrayInputStream(encData)));
+        final ChannelInput channelInput = new ChannelInput(Channels.newChannel(new ByteArrayInputStream(encData)));
         try {
             channelInput.seek(0);
             Assert.fail("Expected UnsupportedOperationException.");
-        } catch (UnsupportedOperationException ex) {
+        } catch (final UnsupportedOperationException ex) {
             Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
         }
         try {
             channelInput.read(0, new byte[0], 0, 0);
             Assert.fail("Expected UnsupportedOperationException.");
-        } catch (UnsupportedOperationException ex) {
+        } catch (final UnsupportedOperationException ex) {
             Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
         }
         Assert.assertEquals(channelInput.available(), 0);
 
-        CtrCryptoInputStream in = new CtrCryptoInputStream(channelInput, getCipher(cipherClass),
+        final CtrCryptoInputStream in = new CtrCryptoInputStream(channelInput, getCipher(cipherClass),
                 defaultBufferSize, key, iv);
 
-        Properties props = new Properties();
-        String bufferSize = "4096";
+        final Properties props = new Properties();
+        final String bufferSize = "4096";
         props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
         in.setStreamOffset(smallBufferSize);
 
@@ -144,7 +144,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
 
         in.close();
 
-        CtrCryptoOutputStream out = new CtrCryptoOutputStream(new ChannelOutput(
+        final CtrCryptoOutputStream out = new CtrCryptoOutputStream(new ChannelOutput(
                 Channels.newChannel(baos)), getCipher(cipherClass),
                 Integer.parseInt(bufferSize), key, iv);
         out.setStreamOffset(smallBufferSize);
@@ -165,15 +165,15 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
     protected void doDecryptTest(final String cipherClass, final boolean withChannel)
             throws IOException {
 
-        CtrCryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream(encData),
+        final CtrCryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream(encData),
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
 
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen);
+        final ByteBuffer buf = ByteBuffer.allocateDirect(dataLen);
         buf.put(encData);
         buf.rewind();
         in.decrypt(buf, 0, dataLen);
-        byte[] readData = new byte[dataLen];
-        byte[] expectedData = new byte[dataLen];
+        final byte[] readData = new byte[dataLen];
+        final byte[] expectedData = new byte[dataLen];
         buf.get(readData);
         System.arraycopy(data, 0, expectedData, 0, dataLen);
         Assert.assertArrayEquals(readData, expectedData);
@@ -181,7 +181,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         try {
             in.decryptBuffer(buf);
             Assert.fail("Expected IOException.");
-        } catch (IOException ex) {
+        } catch (final IOException ex) {
             Assert.assertEquals(ex.getCause().getClass(), ShortBufferException.class);
         }
 
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 1e7ee15..825b00e 100644
--- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -156,7 +156,7 @@ public class PositionedCryptoInputStreamTest {
     }
 
     private void doMultipleReadTest() throws Exception{
-        PositionedCryptoInputStream in = getCryptoInputStream(0);
+        final PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();
         doMultipleReadTest(cipherClass);
     }
@@ -179,7 +179,7 @@ public class PositionedCryptoInputStreamTest {
     }
 
     private void doPositionedReadTests() throws Exception {
-    	PositionedCryptoInputStream in = getCryptoInputStream(0);
+    	final PositionedCryptoInputStream in = getCryptoInputStream(0);
     	final String cipherClass = in.getCipher().getClass().getName();
     	doPositionedReadTests(cipherClass);
     }
@@ -201,7 +201,7 @@ public class PositionedCryptoInputStreamTest {
     }
 
     private void doReadFullyTests() throws Exception {
-        PositionedCryptoInputStream in = getCryptoInputStream(0);
+        final PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();
         doReadFullyTests(cipherClass);
     }
@@ -218,7 +218,7 @@ public class PositionedCryptoInputStreamTest {
     }
 
     private void doSeekTests() throws Exception{
-        PositionedCryptoInputStream in = getCryptoInputStream(0);
+        final PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();
         doSeekTests(cipherClass);
     }


[commons-crypto] 05/07: Remove trailing white space.

Posted by gg...@apache.org.
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

commit f6b718a50142b2156150281d70ebc1893d30ed1b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:27:13 2020 -0400

    Remove trailing white space.
---
 .../commons/crypto/jna/OpenSslJnaCipher.java       |   4 +-
 .../crypto/stream/AbstractCipherStreamTest.java    | 112 ++++++++++-----------
 .../commons/crypto/stream/CtrCryptoStreamTest.java |  50 ++++-----
 .../stream/PositionedCryptoInputStreamTest.java    |  12 +--
 4 files changed, 89 insertions(+), 89 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
index 1c68add..443a892 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
@@ -105,8 +105,8 @@ class OpenSslJnaCipher implements CryptoCipher {
             throw new InvalidAlgorithmParameterException("Illegal parameters");
         }
 
-        if ((algMode == AlgorithmMode.AES_CBC || 
-             algMode == AlgorithmMode.AES_CTR) 
+        if ((algMode == AlgorithmMode.AES_CBC ||
+             algMode == AlgorithmMode.AES_CTR)
             && iv.length != IV_LENGTH) {
             throw new InvalidAlgorithmParameterException("Wrong IV length: must be 16 bytes long");
         }
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 c9db8c4..9f4203e 100644
--- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -103,7 +103,7 @@ public abstract class AbstractCipherStreamTest {
         doByteBufferWrite(AbstractCipherTest.JCE_CIPHER_CLASSNAME, baos, true);
         doByteBufferWrite(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, baos, true);
     }
-    
+
     @Test(timeout = 120000)
     public void testExceptions() throws Exception {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -113,7 +113,7 @@ public abstract class AbstractCipherStreamTest {
         doExceptionTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, baos, true);
         doExceptionTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, baos, true);
     }
-    
+
     @Test(timeout = 120000)
     public void testFieldGetters() throws Exception {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -138,7 +138,7 @@ public abstract class AbstractCipherStreamTest {
             final int n1 = readAll(in, result, 0, dataLen / 5);
 
             Assert.assertEquals(in.skip(0), 0);
-            
+
             long skipped = in.skip(dataLen / 5);
             final int n2 = readAll(in, result, 0, dataLen);
 
@@ -311,7 +311,7 @@ public abstract class AbstractCipherStreamTest {
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
+            final ByteArrayOutputStream baos, final boolean withChannel)
                 throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
@@ -322,17 +322,17 @@ public abstract class AbstractCipherStreamTest {
         CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
         doByteBufferWrite(out, withChannel);
-        
+
         baos.reset();
         CryptoCipher cipher = getCipher(cipherClass);
         String transformation = cipher.getAlgorithm();
-        out = getCryptoOutputStream(transformation, props, baos, key, 
+        out = getCryptoOutputStream(transformation, props, baos, key,
                 new IvParameterSpec(iv), withChannel);
         doByteBufferWrite(out, withChannel);
         out.write(1);
-        Assert.assertTrue(out.isOpen()); 
-        
-        out = getCryptoOutputStream(transformation, props, baos, key, 
+        Assert.assertTrue(out.isOpen());
+
+        out = getCryptoOutputStream(transformation, props, baos, key,
                 new IvParameterSpec(iv), withChannel);
         out.close();
         Assert.assertTrue(!out.isOpen());
@@ -345,84 +345,84 @@ public abstract class AbstractCipherStreamTest {
                 return; // Skip this test if no JNI
             }
         }
-        
+
         InputStream in = null;
         OutputStream out = null;
-        
+
         // Test InvalidAlgorithmParameters
         try {
-        	in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), 
-                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), 
+        	in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData),
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]),
                     withChannel);
             Assert.fail("Expected IOException.");
         } catch (IOException ex) {
             Assert.assertEquals(ex.getMessage(),"Illegal parameters");
-        } 
-        
+        }
+
         // Test InvalidAlgorithmParameters
         try {
-            out = getCryptoOutputStream(transformation, props, baos, 
-                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+            out = getCryptoOutputStream(transformation, props, baos,
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0,
                     new byte[0]), withChannel);
             Assert.fail("Expected IOException.");
         } catch (IOException ex) {
         	Assert.assertEquals(ex.getMessage(),"Illegal parameters");
-        } 
-        
+        }
+
         // Test Invalid Key
         try {
-            in = getCryptoInputStream(transformation,props, new ByteArrayInputStream(encData), 
+            in = getCryptoInputStream(transformation,props, new ByteArrayInputStream(encData),
                     new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel);
             Assert.fail("Expected IOException for Invalid Key");
         } catch (IOException ex) {
             Assert.assertNotNull(ex);
         }
-        
+
         // Test Invalid Key
         try {
-            out = getCryptoOutputStream(transformation, props, baos, new byte[10], 
+            out = getCryptoOutputStream(transformation, props, baos, new byte[10],
                     new IvParameterSpec(iv), withChannel);
             Assert.fail("Expected IOException for Invalid Key");
         } catch (IOException ex) {
             Assert.assertNotNull(ex);
         }
-        
+
         // Test reading a closed stream.
-        try { 
-            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+        try {
+            in = getCryptoInputStream(new ByteArrayInputStream(encData),
                     getCipher(cipherClass), defaultBufferSize, iv, withChannel);
             in.close();
             in.read(); // Throw exception.
         } catch (IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("Stream closed"));
-        } 
-        
+        }
+
         // Test closing a closed stream.
-        try { 
+        try {
             in.close(); // Don't throw exception on double-close.
         } catch (IOException ex) {
             Assert.fail("Should not throw exception closing a closed stream.");
-        } 
+        }
 
         // Test checking a closed stream.
-        try { 
-            out = getCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), 
+        try {
+            out = getCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv),
                     withChannel);
             out.close();
             ((CryptoOutputStream)out).checkStream(); // Throw exception.
         } catch (IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("Stream closed"));
-        } 
+        }
 
         // Test closing a closed stream.
-        try { 
+        try {
             out.close(); // Don't throw exception.
         } catch (IOException ex) {
             Assert.fail("Should not throw exception closing a closed stream.");
-        } 
-        
+        }
+
         // Test checkStreamCipher
-        try { 
+        try {
             CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
         } catch (IOException ex) {
             Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is required"));
@@ -431,8 +431,8 @@ public abstract class AbstractCipherStreamTest {
         }
 
         // Test unsupported operation handling.
-        try { 
-            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+        try {
+            in = getCryptoInputStream(new ByteArrayInputStream(encData),
                     getCipher(cipherClass), defaultBufferSize, iv, false);
             in.mark(0);
             assertEquals(false, in.markSupported());
@@ -452,12 +452,12 @@ public abstract class AbstractCipherStreamTest {
                 return; // Skip this test if no JNI
             }
         }
-        
+
         CryptoCipher cipher = getCipher(cipherClass);
-        
+
         CryptoInputStream in = getCryptoInputStream(
-                new ByteArrayInputStream(encData), cipher, defaultBufferSize, 
-                iv, withChannel); 
+                new ByteArrayInputStream(encData), cipher, defaultBufferSize,
+                iv, withChannel);
 
         Properties props = new Properties();
         String bufferSize = Integer.toString(defaultBufferSize / 2);
@@ -470,14 +470,14 @@ public abstract class AbstractCipherStreamTest {
         Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
         Assert.assertNotNull(in.getInput());
 
-        CryptoOutputStream out = getCryptoOutputStream(baos, getCipher(cipherClass), 
+        CryptoOutputStream out = getCryptoOutputStream(baos, getCipher(cipherClass),
                 defaultBufferSize, iv, withChannel);
 
         Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + cipher.getBlockSize());
         Assert.assertEquals(out.getInBuffer().capacity(), defaultBufferSize);
         Assert.assertEquals(out.getBufferSize(), defaultBufferSize);
     }
-    
+
     private void byteBufferReadCheck(final InputStream in, final ByteBuffer buf, final int bufPos)
             throws Exception {
         buf.position(bufPos);
@@ -535,7 +535,7 @@ public abstract class AbstractCipherStreamTest {
         }
         encData = baos.toByteArray();
     }
-    
+
     private void doByteBufferWrite(CryptoOutputStream out, boolean withChannel) throws Exception {
         ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
         buf.put(data, 0, dataLen / 2);
@@ -551,18 +551,18 @@ public abstract class AbstractCipherStreamTest {
         buf.put(data, n1 + n2, dataLen - n1 - n2 - 1);
         buf.flip();
         final int n3 = out.write(buf);
-        
+
         out.write(1);
 
         Assert.assertEquals(dataLen, n1 + n2 + n3 + 1);
-        
+
         try {
             out.write(data, 0, data.length + 1);
             Assert.fail("Expected IndexOutOfBoundsException.");
         } catch (IndexOutOfBoundsException ex) {
             Assert.assertNotNull(ex);
         }
-        
+
         out.flush();
 
         try (InputStream in = getCryptoInputStream(
@@ -584,8 +584,8 @@ public abstract class AbstractCipherStreamTest {
         return new CryptoInputStream(bais, cipher, bufferSize,
                 new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
     }
-    
-    protected CryptoInputStream getCryptoInputStream(final String transformation, final Properties props, 
+
+    protected CryptoInputStream getCryptoInputStream(final String transformation, final Properties props,
     	    final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
     	    boolean withChannel) throws IOException {
         if (withChannel) {
@@ -593,7 +593,7 @@ public abstract class AbstractCipherStreamTest {
     	}
         return new CryptoInputStream(transformation, props, bais, new SecretKeySpec(key, "AES"), params);
     }
-    
+
     protected CryptoInputStream getCryptoInputStream(final String transformation,
             final Properties props, final ByteArrayInputStream bais, final Key key,
             final AlgorithmParameterSpec params, boolean withChannel) throws IOException {
@@ -614,18 +614,18 @@ public abstract class AbstractCipherStreamTest {
         return new CryptoOutputStream(baos, cipher, bufferSize,
                 new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
     }
-    
+
     protected CryptoOutputStream getCryptoOutputStream(final String transformation,
-            final Properties props, final ByteArrayOutputStream baos, final byte[] key, 
+            final Properties props, final ByteArrayOutputStream baos, final byte[] key,
             final AlgorithmParameterSpec param, final boolean withChannel) throws IOException {
         if (withChannel) {
-            return new CryptoOutputStream(transformation, props, Channels.newChannel(baos), 
+            return new CryptoOutputStream(transformation, props, Channels.newChannel(baos),
                     new SecretKeySpec(key, "AES"), param);
         }
-        return new CryptoOutputStream(transformation, props, baos, new SecretKeySpec(key, "AES"), 
+        return new CryptoOutputStream(transformation, props, baos, new SecretKeySpec(key, "AES"),
                 param);
     }
-    
+
     protected CryptoOutputStream getCryptoOutputStream(final String transformation,
             final Properties props, final ByteArrayOutputStream baos, final Key key,
             final AlgorithmParameterSpec params, boolean withChannel) throws IOException {
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 22e16e9..5d7d14e 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
@@ -53,13 +53,13 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         }
         return new CtrCryptoInputStream(bais, cipher, bufferSize, key, iv);
     }
-    
+
     @Override
-    protected CtrCryptoInputStream getCryptoInputStream(final String transformation, final Properties props, 
+    protected CtrCryptoInputStream getCryptoInputStream(final String transformation, final Properties props,
             final ByteArrayInputStream bais, final byte[] key, final AlgorithmParameterSpec params,
             boolean withChannel) throws IOException {
         if (withChannel) {
-            return new CtrCryptoInputStream(props, Channels.newChannel(bais), key, 
+            return new CtrCryptoInputStream(props, Channels.newChannel(bais), key,
                     ((IvParameterSpec)params).getIV());
         }
         return new CtrCryptoInputStream(props, bais, key, ((IvParameterSpec)params).getIV());
@@ -75,18 +75,18 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         }
         return new CtrCryptoOutputStream(baos, cipher, bufferSize, key, iv);
     }
-    
+
     @Override
     protected CtrCryptoOutputStream getCryptoOutputStream(final String transformation,
-            final Properties props, final ByteArrayOutputStream baos, final byte[] key, 
+            final Properties props, final ByteArrayOutputStream baos, final byte[] key,
             final AlgorithmParameterSpec params, final boolean withChannel) throws IOException {
         if (withChannel) {
-            return new CtrCryptoOutputStream(props, Channels.newChannel(baos), key, 
+            return new CtrCryptoOutputStream(props, Channels.newChannel(baos), key,
                     ((IvParameterSpec)params).getIV());
         }
         return new CtrCryptoOutputStream(props, baos, key, ((IvParameterSpec)params).getIV());
     }
-    
+
     @Override
     protected void doFieldGetterTest(final String cipherClass, final ByteArrayOutputStream baos,
             final boolean withChannel) throws Exception {
@@ -95,7 +95,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
                 return; // Skip this test if no JNI
             }
         }
-        
+
         StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
         try {
             streamInput.seek(0);
@@ -110,7 +110,7 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
             Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
         }
         Assert.assertEquals(streamInput.available(), encData.length);
-        
+
         ChannelInput channelInput = new ChannelInput(Channels.newChannel(new ByteArrayInputStream(encData)));
         try {
             channelInput.seek(0);
@@ -125,15 +125,15 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
             Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
         }
         Assert.assertEquals(channelInput.available(), 0);
-        
-        CtrCryptoInputStream in = new CtrCryptoInputStream(channelInput, getCipher(cipherClass), 
+
+        CtrCryptoInputStream in = new CtrCryptoInputStream(channelInput, getCipher(cipherClass),
                 defaultBufferSize, key, iv);
-        
+
         Properties props = new Properties();
         String bufferSize = "4096";
         props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
         in.setStreamOffset(smallBufferSize);
-       
+
         Assert.assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
         Assert.assertEquals(smallBufferSize, in.getStreamOffset());
         Assert.assertEquals(in.getBufferSize(), 8192);
@@ -141,18 +141,18 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         Assert.assertEquals(in.getKey().getAlgorithm(), "AES");
         Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
         Assert.assertNotNull(in.getInput());
-    	
+
         in.close();
-    	
+
         CtrCryptoOutputStream out = new CtrCryptoOutputStream(new ChannelOutput(
-                Channels.newChannel(baos)), getCipher(cipherClass), 
+                Channels.newChannel(baos)), getCipher(cipherClass),
                 Integer.parseInt(bufferSize), key, iv);
         out.setStreamOffset(smallBufferSize);
         Assert.assertEquals(out.getStreamOffset(), smallBufferSize);
-        
+
         out.close();
     }
-    
+
     @Test(timeout = 120000)
     public void testDecrypt() throws Exception {
         doDecryptTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
@@ -161,13 +161,13 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         doDecryptTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, true);
         doDecryptTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, true);
     }
-    
+
     protected void doDecryptTest(final String cipherClass, final boolean withChannel)
             throws IOException {
-    	
-        CtrCryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+
+        CtrCryptoInputStream in = getCryptoInputStream(new ByteArrayInputStream(encData),
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-    	
+
         ByteBuffer buf = ByteBuffer.allocateDirect(dataLen);
         buf.put(encData);
         buf.rewind();
@@ -177,13 +177,13 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
         buf.get(readData);
         System.arraycopy(data, 0, expectedData, 0, dataLen);
         Assert.assertArrayEquals(readData, expectedData);
-        
+
         try {
             in.decryptBuffer(buf);
             Assert.fail("Expected IOException.");
         } catch (IOException ex) {
-            Assert.assertEquals(ex.getCause().getClass(), ShortBufferException.class);	
+            Assert.assertEquals(ex.getCause().getClass(), ShortBufferException.class);
         }
-        
+
     }
 }
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 6293720..1e7ee15 100644
--- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -93,8 +93,8 @@ public class PositionedCryptoInputStreamTest {
                 Arrays.copyOf(encData, encData.length)), cipher, bufferSize,
                 key, iv, 0);
     }
-    
-    private PositionedCryptoInputStream getCryptoInputStream(final int streamOffset) 
+
+    private PositionedCryptoInputStream getCryptoInputStream(final int streamOffset)
             throws IOException {
         return new PositionedCryptoInputStream(props, new PositionedInputForTest(
                 Arrays.copyOf(encData, encData.length)), key, iv, streamOffset);
@@ -154,7 +154,7 @@ public class PositionedCryptoInputStreamTest {
             }
         }
     }
-    
+
     private void doMultipleReadTest() throws Exception{
         PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();
@@ -177,7 +177,7 @@ public class PositionedCryptoInputStreamTest {
         testPositionedReadNone(cipherClass, -1, length, bufferSize);
         testPositionedReadNone(cipherClass, dataLen, length, bufferSize);
     }
-    
+
     private void doPositionedReadTests() throws Exception {
     	PositionedCryptoInputStream in = getCryptoInputStream(0);
     	final String cipherClass = in.getCipher().getClass().getName();
@@ -199,7 +199,7 @@ public class PositionedCryptoInputStreamTest {
         testReadFullyFailed(cipherClass, dataLen - length + 1, length,
                 bufferSize);
     }
-    
+
     private void doReadFullyTests() throws Exception {
         PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();
@@ -216,7 +216,7 @@ public class PositionedCryptoInputStreamTest {
         // test exception when position = -1
         testSeekFailed(cipherClass, -1, bufferSize);
     }
-    
+
     private void doSeekTests() throws Exception{
         PositionedCryptoInputStream in = getCryptoInputStream(0);
         final String cipherClass = in.getCipher().getClass().getName();


[commons-crypto] 07/07: Add missing annotations.

Posted by gg...@apache.org.
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

commit 4cfe1717f7d53415e146e21ae28a3b0305d98679
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:29:37 2020 -0400

    Add missing annotations.
---
 src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java | 3 +++
 1 file changed, 3 insertions(+)

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 279ff3a..b5fa094 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
@@ -113,6 +113,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
         }
     }
 
+    @Override
     @Test(expected = InvalidKeyException.class, timeout = 120000)
     public void testInvalidKey() throws Exception {
         Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
@@ -125,6 +126,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
         cipher.init(OpenSsl.ENCRYPT_MODE, invalidKey, new IvParameterSpec(IV));
     }
 
+    @Override
     @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
     public void testInvalidIV() throws Exception {
         Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
@@ -137,6 +139,7 @@ public class OpenSslCipherTest extends AbstractCipherTest {
         cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(invalidIV));
     }
 
+    @Override
     @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
     public void testInvalidIVClass() throws Exception {
         final OpenSsl cipher = OpenSsl.getInstance("AES/CTR/NoPadding");


[commons-crypto] 02/07: Fix Javadoc.

Posted by gg...@apache.org.
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

commit 0e7084d6b6bc5ed9c2b38e806bf688a6bf4f1609
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:18:46 2020 -0400

    Fix Javadoc.
---
 src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

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 b4e0844..05ab89c 100644
--- a/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/random/JavaCryptoRandom.java
@@ -33,11 +33,9 @@ class JavaCryptoRandom extends Random implements CryptoRandom {
      * Constructs a {@link JavaCryptoRandom}.
      *
      * @param properties the configuration properties.
-     * Uses the key {@link CryptoRandomFactory#SECURE_RANDOM_JAVA_ALGORITHM_KEY}
+     * Uses the key {@link CryptoRandomFactory#JAVA_ALGORITHM_KEY}
      * to get the name of the algorithm, with a default of
      * {@link CryptoRandomFactory#JAVA_ALGORITHM_DEFAULT}
-     * @throws NoSuchAlgorithmException if no Provider supports a
-     *         SecureRandomSpi implementation for the specified algorithm.
      */
     // N.B. this class is not public/protected so does not appear in the main Javadoc
     // Please ensure that property use is documented in the enum CryptoRandomFactory.RandomProvider


[commons-crypto] 03/07: Remove unused imports.

Posted by gg...@apache.org.
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

commit b2eb1fcec0cb1db460e988dbe84344a4d47c153b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:20:31 2020 -0400

    Remove unused imports.
---
 .../java/org/apache/commons/crypto/cipher/AbstractCipherTest.java     | 4 +---
 src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java | 1 -
 2 files changed, 1 insertion(+), 4 deletions(-)

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 b87c6d5..3ae06a0 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -19,19 +19,17 @@ package org.apache.commons.crypto.cipher;
 
 import static org.junit.Assert.assertNotNull;
 
-import java.lang.reflect.InvocationTargetException;
 import java.nio.ByteBuffer;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.util.Properties;
 import java.util.Random;
 
 import javax.crypto.Cipher;
+import javax.crypto.spec.GCMParameterSpec;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
-import javax.crypto.spec.GCMParameterSpec;
 import javax.xml.bind.DatatypeConverter;
 
 import org.apache.commons.crypto.utils.ReflectionUtils;
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 b90e8a0..91929aa 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
@@ -20,7 +20,6 @@ package org.apache.commons.crypto.jna;
 
 import org.apache.commons.crypto.cipher.AbstractCipherTest;
 import org.junit.Assume;
-import org.junit.Test;
 
 public class OpenSslJnaCipherTest extends AbstractCipherTest {
 


[commons-crypto] 04/07: Use try-with-resources.

Posted by gg...@apache.org.
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

commit 98608002dc4450155b98a528a9f4bee02a493e55
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 6 19:25:51 2020 -0400

    Use try-with-resources.
---
 .../crypto/stream/AbstractCipherStreamTest.java    | 215 ++++++++++-----------
 1 file changed, 107 insertions(+), 108 deletions(-)

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 8d585a6..c9db8c4 100644
--- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -163,152 +163,151 @@ public abstract class AbstractCipherStreamTest {
     }
 
     protected void doByteBufferRead(final String cipherClass, final boolean withChannel)
-            throws Exception {
+        throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
-        // Default buffer size, initial buffer position is 0
-        InputStream in = getCryptoInputStream(
-                new ByteArrayInputStream(encData), getCipher(cipherClass),
-                defaultBufferSize, iv, withChannel);
         ByteBuffer buf = ByteBuffer.allocate(dataLen + 100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        // Default buffer size, initial buffer position is 0
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            defaultBufferSize, iv, withChannel)) {
+            byteBufferReadCheck(in, buf, 0);
+        }
 
         // Default buffer size, initial buffer position is not 0
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            defaultBufferSize, iv, withChannel)) {
+            buf.clear();
+            byteBufferReadCheck(in, buf, 11);
+        }
 
         // Small buffer size, initial buffer position is 0
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), smallBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            smallBufferSize, iv, withChannel)) {
+            buf.clear();
+            byteBufferReadCheck(in, buf, 0);
+        }
 
         // Small buffer size, initial buffer position is not 0
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), smallBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            smallBufferSize, iv, withChannel)) {
+            buf.clear();
+            byteBufferReadCheck(in, buf, 11);
+        }
 
         // Direct buffer, default buffer size, initial buffer position is 0
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        buf = ByteBuffer.allocateDirect(dataLen + 100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            defaultBufferSize, iv, withChannel)) {
+            buf.clear();
+            byteBufferReadCheck(in, buf, 11);
+        }
 
         // Direct buffer, small buffer size, initial buffer position is 0
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), smallBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), smallBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();      
-        
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), smallBufferSize, iv, withChannel);
-        buf.clear();
-        byteBufferFinalReadCheck(in, buf, 0);
-        in.close();
-        
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(new ByteArrayInputStream(encData),
-                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        buf = ByteBuffer.allocate(100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
-        
+        try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass),
+            defaultBufferSize, iv, withChannel)) {
+            buf = ByteBuffer.allocate(100);
+            byteBufferReadCheck(in, buf, 0);
+        }
+
         // Default buffer size, initial buffer position is 0
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf = ByteBuffer.allocate(dataLen + 100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
-        
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key,
+            new IvParameterSpec(iv), withChannel)) {
+            buf.clear();
+            byteBufferReadCheck(in, buf, 11);
+        }
 
         // Small buffer size, initial buffer position is 0
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf = ByteBuffer.allocateDirect(dataLen + 100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferReadCheck(in, buf, 11);
-        in.close();
-        
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf.clear();
-        byteBufferFinalReadCheck(in, buf, 0);
-        in.close();
-        
+        try (InputStream in = getCryptoInputStream(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
-        in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, 
-                new IvParameterSpec(iv), withChannel);
-        buf = ByteBuffer.allocate(100);
-        byteBufferReadCheck(in, buf, 0);
-        in.close();
+        try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key,
+            new IvParameterSpec(iv), withChannel)) {
+            buf = ByteBuffer.allocate(100);
+            byteBufferReadCheck(in, buf, 0);
+        }
     }
 
     protected void doByteBufferWrite(final String cipherClass,