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 e06fefd Use try-with-resources.
e06fefd is described below
commit e06fefd494a1c4945f8795b772480b0f2571755b
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Mar 17 10:10:07 2022 -0400
Use try-with-resources.
---
.../commons/crypto/cipher/AbstractCipherTest.java | 47 ++++++++--------
.../crypto/examples/CipherByteArrayExample.java | 64 ++++++++++++----------
.../crypto/stream/AbstractCipherStreamTest.java | 18 +++---
.../stream/PositionedCryptoInputStreamTest.java | 29 +++++-----
4 files changed, 83 insertions(+), 75 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 6233fb2..15b8398 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -77,49 +77,50 @@ public abstract class AbstractCipherTest {
public void closeTestNoInit() throws Exception {
// This test deliberately does not use try with resources in
order to control
// the sequence of operations exactly
- final CryptoCipher enc = getCipher(transformations[0]);
- enc.close();
+ try (final CryptoCipher enc = getCipher(transformations[0])) {
+ // empty
+ }
}
@Test
public void closeTestAfterInit() throws Exception {
// This test deliberately does not use try with resources in
order to control
// the sequence of operations exactly
- final CryptoCipher enc = getCipher(transformations[0]);
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.close();
+ try (final CryptoCipher enc = getCipher(transformations[0])) {
+ enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ }
}
@Test
public void reInitTest() throws Exception {
// This test deliberately does not use try with resources in
order to control
// the sequence of operations exactly
- final CryptoCipher enc = getCipher(transformations[0]);
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.close();
+ try (final CryptoCipher enc = getCipher(transformations[0])) {
+ enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ }
}
@Test
- public void reInitAfterClose() throws Exception {
- // This test deliberately does not use try with resources in
order to control
- // the sequence of operations exactly
- final CryptoCipher enc = getCipher(transformations[0]);
- enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.close();
- enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
- enc.close();
- }
+ public void reInitAfterClose() throws Exception {
+ // This test deliberately does not use try with resources in order to
control
+ // the sequence of operations exactly
+ try (final CryptoCipher enc = getCipher(transformations[0])) {
+ enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ enc.close();
+ enc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES"), new
IvParameterSpec(IV));
+ }
+ }
@Test
public void closeTestRepeat() throws Exception {
// This test deliberately does not use try with resources in
order to control
// the sequence of operations exactly
- final CryptoCipher enc = getCipher(transformations[0]);
- enc.close();
- enc.close(); // repeat the close
- enc.close();
+ try (final CryptoCipher enc = getCipher(transformations[0])) {
+ enc.close();
+ enc.close(); // repeat the close
+ }
}
@Test
diff --git
a/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
index 264466f..15afb8d 100644
---
a/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
+++
b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java
@@ -37,45 +37,51 @@ public class CipherByteArrayExample {
public static void main(final String[] args) throws Exception {
- final SecretKeySpec key = new
SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES");
+ final SecretKeySpec key = new
SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
final IvParameterSpec iv = new
IvParameterSpec(getUTF8Bytes("1234567890123456"));
final Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
CipherProvider.OPENSSL.getClassName());
- //Creates a CryptoCipher instance with the transformation and
properties.
+ // Creates a CryptoCipher instance with the transformation and
properties.
final String transform = "AES/CBC/PKCS5Padding";
- final CryptoCipher encipher = Utils.getCipherInstance(transform,
properties);
- System.out.println("Cipher: " +
encipher.getClass().getCanonicalName());
-
- final String sampleInput = "hello world!";
- System.out.println("input: " + sampleInput);
-
- final byte[] input = getUTF8Bytes(sampleInput);
- final byte[] output = new byte[32];
-
- //Initializes the cipher with ENCRYPT_MODE, key and iv.
- encipher.init(Cipher.ENCRYPT_MODE, key, iv);
- //Continues a multiple-part encryption/decryption operation for byte
array.
- final int updateBytes = encipher.update(input, 0, input.length,
output, 0);
- System.out.println(updateBytes);
- //We must call doFinal at the end of encryption/decryption.
- final int finalBytes = encipher.doFinal(input, 0, 0, output,
updateBytes);
- System.out.println(finalBytes);
- //Closes the cipher.
- encipher.close();
-
- System.out.println(Arrays.toString(Arrays.copyOf(output,
updateBytes+finalBytes)));
+ byte[] output;
+ int updateBytes;
+ int finalBytes;
+ Class<?> encipherClass;
+ try (final CryptoCipher encipher = Utils.getCipherInstance(transform,
properties)) {
+ System.out.println("Cipher: " +
encipher.getClass().getCanonicalName());
+
+ final String sampleInput = "hello world!";
+ System.out.println("input: " + sampleInput);
+
+ final byte[] input = getUTF8Bytes(sampleInput);
+ output = new byte[32];
+
+ // Initializes the cipher with ENCRYPT_MODE, key and iv.
+ encipher.init(Cipher.ENCRYPT_MODE, key, iv);
+ // Continues a multiple-part encryption/decryption operation for
byte array.
+ updateBytes = encipher.update(input, 0, input.length, output, 0);
+ System.out.println(updateBytes);
+ // We must call doFinal at the end of encryption/decryption.
+ finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
+ System.out.println(finalBytes);
+ encipherClass = encipher.getClass();
+ // Closes the cipher.
+ }
+
+ System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes +
finalBytes)));
// Now reverse the process using a different implementation with the
same settings
properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
CipherProvider.JCE.getClassName());
- final CryptoCipher decipher = Utils.getCipherInstance(transform,
properties);
- System.out.println("Cipher: " +
encipher.getClass().getCanonicalName());
+ try (final CryptoCipher decipher = Utils.getCipherInstance(transform,
properties)) {
+ System.out.println("Cipher: " + encipherClass.getCanonicalName());
- decipher.init(Cipher.DECRYPT_MODE, key, iv);
- final byte [] decoded = new byte[32];
- decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);
+ decipher.init(Cipher.DECRYPT_MODE, key, iv);
+ final byte[] decoded = new byte[32];
+ decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);
- System.out.println("output: " + new String(decoded,
StandardCharsets.UTF_8));
+ System.out.println("output: " + new String(decoded,
StandardCharsets.UTF_8));
+ }
}
/**
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 547c3c1..600dfae 100644
---
a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++
b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -365,16 +365,18 @@ public abstract class AbstractCipherStreamTest {
new IvParameterSpec(iv), withChannel));
// Test reading a closed stream.
- final InputStream in = newCryptoInputStream(new
ByteArrayInputStream(encData),
- getCipher(cipherClass), defaultBufferSize, iv, withChannel);
- in.close();
+ InputStream closedIn;
+ try (final InputStream in = newCryptoInputStream(new
ByteArrayInputStream(encData),
+ getCipher(cipherClass), defaultBufferSize, iv, withChannel)) {
+ closedIn = in;
+ }
// Throw exception.
- ex = assertThrows(IOException.class, in::read);
+ ex = assertThrows(IOException.class, closedIn::read);
assertEquals(ex.getMessage(), "Stream closed");
// Test closing a closed stream.
try {
- in.close(); // Don't throw exception on double-close.
+ closedIn.close(); // Don't throw exception on double-close.
} catch (final IOException ioEx) {
fail("Should not throw exception closing a closed stream.");
}
@@ -399,14 +401,14 @@ public abstract class AbstractCipherStreamTest {
} catch (final IOException ioEx) {
assertEquals(ioEx.getMessage(), "AES/CTR/NoPadding is required");
} finally {
- in.close();
+ closedIn.close();
}
// Test unsupported operation handling.
final InputStream inNewCrytptoStr = newCryptoInputStream(new
ByteArrayInputStream(encData),
getCipher(cipherClass), defaultBufferSize, iv, false);
- in.mark(0);
- assertFalse(in.markSupported());
+ closedIn.mark(0);
+ assertFalse(closedIn.markSupported());
ex = assertThrows(IOException.class, inNewCrytptoStr::reset);
assertEquals(ex.getMessage(), "mark/reset not supported");
}
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 418347d..0926b9e 100644
---
a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++
b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -82,11 +82,11 @@ public class PositionedCryptoInputStreamTest {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// encryption data
- final OutputStream out = new CryptoOutputStream(baos, cipher,
bufferSize,
- new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
- out.write(testData);
- out.flush();
- out.close();
+ try (final OutputStream out = new CryptoOutputStream(baos, cipher,
bufferSize,
+ new SecretKeySpec(key, "AES"), new IvParameterSpec(iv))) {
+ out.write(testData);
+ out.flush();
+ }
encData = baos.toByteArray();
}
@@ -246,10 +246,9 @@ public class PositionedCryptoInputStreamTest {
// test for the out of index position, eg, -1.
private void testSeekFailed(final String cipherClass, final int position,
final int bufferSize)
throws Exception {
- final PositionedCryptoInputStream in = getCryptoInputStream(
- getCipher(cipherClass), bufferSize);
- assertThrows(IllegalArgumentException.class, () -> in.seek(position));
- in.close();
+ try (final PositionedCryptoInputStream in =
getCryptoInputStream(getCipher(cipherClass), bufferSize)) {
+ assertThrows(IllegalArgumentException.class, () ->
in.seek(position));
+ }
}
private void testPositionedReadLoop(final String cipherClass, int position,
@@ -298,12 +297,12 @@ public class PositionedCryptoInputStreamTest {
// test for the End of file reached before reading fully
private void testReadFullyFailed(final String cipherClass, final int
position,
final int length, final int bufferSize) throws Exception {
- final PositionedCryptoInputStream in = getCryptoInputStream(
- getCipher(cipherClass), bufferSize);
- final byte[] bytes = new byte[length];
- assertThrows(IOException.class, () -> in.readFully(position, bytes, 0,
length));
- in.close();
- in.close(); // Don't throw exception.
+ try (final PositionedCryptoInputStream in =
getCryptoInputStream(getCipher(cipherClass), bufferSize)) {
+ final byte[] bytes = new byte[length];
+ assertThrows(IOException.class, () -> in.readFully(position,
bytes, 0, length));
+ in.close();
+ in.close(); // Don't throw exception.
+ }
}
// compare the data from pos with length and data2 from 0 with length