You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/12/11 15:51:26 UTC

[commons-crypto] branch master updated: Account for spirit of Add isEmpty method check if an String is empty or null. #133 with a new public method

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 9d62e9d  Account for spirit of Add isEmpty method check if an String is empty or null. #133 with a new public method
9d62e9d is described below

commit 9d62e9d666a83de00d1d32711549698fb8536341
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 11 10:51:13 2022 -0500

    Account for spirit of Add isEmpty method check if an String is empty or
    null. #133 with a new public method
---
 .../java/org/apache/commons/crypto/cipher/JceCipher.java   | 14 ++++++--------
 .../apache/commons/crypto/stream/CryptoInputStream.java    | 13 ++++---------
 2 files changed, 10 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java b/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
index 8bdb493..3a81922 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/JceCipher.java
@@ -33,6 +33,10 @@ import javax.crypto.ShortBufferException;
 
 /**
  * Implements the {@link CryptoCipher} using JCE provider.
+ * <p>
+ * 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
+ * </p>
  */
 class JceCipher implements CryptoCipher {
     private final Cipher cipher;
@@ -44,16 +48,10 @@ class JceCipher implements CryptoCipher {
      * @param transformation  transformation for JCE cipher (algorithm/mode/padding)
      * @throws GeneralSecurityException if JCE cipher initialize failed
      */
-    // 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
     public JceCipher(final Properties props, final String transformation)
             throws GeneralSecurityException {
-        final String provider = props.getProperty(CryptoCipherFactory.JCE_PROVIDER_KEY);
-        if (provider == null || provider.isEmpty()) {
-            cipher = Cipher.getInstance(transformation);
-        } else {
-            cipher = Cipher.getInstance(transformation, provider);
-        }
+        final String provider = props.getProperty(CryptoCipherFactory.JCE_PROVIDER_KEY, "");
+        cipher = provider.isEmpty() ? Cipher.getInstance(transformation) : Cipher.getInstance(transformation, provider);
     }
 
     /**
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 95c0b38..bf939b0 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
@@ -618,11 +618,8 @@ public class CryptoInputStream extends InputStream implements
      * @return the buffer size.
      * */
     static int getBufferSize(final Properties props) {
-        final String bufferSizeStr = props.getProperty(CryptoInputStream.STREAM_BUFFER_SIZE_KEY);
-        if (bufferSizeStr == null || bufferSizeStr.isEmpty()) {
-            return CryptoInputStream.STREAM_BUFFER_SIZE_DEFAULT;
-        }
-        return Integer.parseInt(bufferSizeStr);
+        final String bufferSizeStr = props.getProperty(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, "");
+        return bufferSizeStr.isEmpty() ? CryptoInputStream.STREAM_BUFFER_SIZE_DEFAULT : Integer.parseInt(bufferSizeStr);
     }
 
     /**
@@ -631,8 +628,7 @@ public class CryptoInputStream extends InputStream implements
      * @param cipher the {@link CryptoCipher} instance.
      * @throws IOException if an I/O error occurs.
      */
-    static void checkStreamCipher(final CryptoCipher cipher)
-            throws IOException {
+    static void checkStreamCipher(final CryptoCipher cipher) throws IOException {
         if (!cipher.getAlgorithm().equals(AES.CTR_NO_PADDING)) {
             throw new IOException(AES.CTR_NO_PADDING + " is required");
         }
@@ -648,7 +644,6 @@ public class CryptoInputStream extends InputStream implements
     static int checkBufferSize(final CryptoCipher cipher, final int bufferSize) {
         Utils.checkArgument(bufferSize >= CryptoInputStream.MIN_BUFFER_SIZE,
                 "Minimum value of buffer size is " + CryptoInputStream.MIN_BUFFER_SIZE + ".");
-        return bufferSize - bufferSize
-                % cipher.getBlockSize();
+        return bufferSize - bufferSize % cipher.getBlockSize();
     }
 }