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/12 16:32:42 UTC
[commons-crypto] 01/02: Refactor duplicate code
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
commit 0040f46cdfd15831965a24c4347d22fbbc0dcd8e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 12 11:31:12 2022 -0500
Refactor duplicate code
---
.../org/apache/commons/crypto/cipher/OpenSsl.java | 3 +-
.../commons/crypto/jna/OpenSslJnaCipher.java | 5 ++--
.../commons/crypto/jna/OpenSslNativeJna.java | 16 ++++------
.../org/apache/commons/crypto/utils/Padding.java | 13 ++++++---
.../commons/crypto/utils/Transformation.java | 34 +++++++++++++++++-----
5 files changed, 43 insertions(+), 28 deletions(-)
diff --git a/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java b/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
index 5c58c2d..c97f392 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
@@ -28,7 +28,6 @@ import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import org.apache.commons.crypto.Crypto;
-import org.apache.commons.crypto.utils.Padding;
import org.apache.commons.crypto.utils.Transformation;
import org.apache.commons.crypto.utils.Utils;
@@ -127,7 +126,7 @@ final class OpenSsl {
}
final Transformation transform = Transformation.parse(transformation);
final int algorithmMode = AlgorithmMode.get(transform.getAlgorithm(), transform.getMode());
- final int padding = Padding.get(transform.getPadding());
+ final int padding = transform.getPadding().ordinal();
final long context = OpenSslNative.initContext(algorithmMode, padding);
return new OpenSsl(context, algorithmMode, padding);
}
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 e1894ec..94b14b5 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
@@ -35,7 +35,6 @@ import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
-import org.apache.commons.crypto.utils.Padding;
import org.apache.commons.crypto.utils.Transformation;
import com.sun.jna.NativeLong;
@@ -70,10 +69,10 @@ class OpenSslJnaCipher implements CryptoCipher {
algorithmMode = AlgorithmMode.get(transform.getAlgorithm(), transform.getMode());
if (algorithmMode != AlgorithmMode.AES_CBC && algorithmMode != AlgorithmMode.AES_CTR) {
- throw new GeneralSecurityException("unknown algorithm " + transform.getAlgorithm() + "_" + transform.getMode());
+ throw new GeneralSecurityException("Unknown algorithm " + transform.getAlgorithm() + "_" + transform.getMode());
}
- padding = Padding.get(transform.getPadding());
+ padding = transform.getPadding().ordinal();
context = OpenSslNativeJna.EVP_CIPHER_CTX_new();
}
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 f5ab128..ea7522d 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
@@ -73,11 +73,7 @@ class OpenSslNativeJna {
INIT_OK = JnaImplementation._INIT_OK();
- if (INIT_OK) {
- INIT_ERROR = null;
- } else {
- INIT_ERROR = JnaImplementation._INIT_ERROR();
- }
+ INIT_ERROR = INIT_OK ? null : JnaImplementation._INIT_ERROR();
}
private OpenSslNativeJna() {
@@ -143,9 +139,8 @@ class OpenSslNativeJna {
return JnaImplementation._EVP_CIPHER_CTX_new();
}
- // TODO: native method returns int
- public static void EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
- JnaImplementation._EVP_CIPHER_CTX_set_padding(context, padding);
+ public static int EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
+ return JnaImplementation._EVP_CIPHER_CTX_set_padding(context, padding);
}
public static int EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer,
@@ -184,9 +179,8 @@ class OpenSslNativeJna {
JnaImplementation._ENGINE_load_rdrand();
}
- // TODO: native method returns int
- public static void ENGINE_cleanup() {
- JnaImplementation._ENGINE_cleanup();
+ public static int ENGINE_cleanup() {
+ return JnaImplementation._ENGINE_cleanup();
}
public static void EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
diff --git a/src/main/java/org/apache/commons/crypto/utils/Padding.java b/src/main/java/org/apache/commons/crypto/utils/Padding.java
index 445f19a..59d82e4 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Padding.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Padding.java
@@ -25,20 +25,25 @@ import javax.crypto.NoSuchPaddingException;
*/
public enum Padding {
- NoPadding, PKCS5Padding;
+ /** Don't change the order of this enum value. */
+ NoPadding,
+
+ /** Don't change the order of this enum value. */
+ PKCS5Padding;
/**
* Gets a Padding.
*
* @param padding the padding name.
* @return a Padding instance.
- * @throws NoSuchPaddingException if the algorithm is not support
+ * @throws NoSuchPaddingException if the algorithm is not supported.
*/
- public static int get(final String padding) throws NoSuchPaddingException {
+ public static Padding get(final String padding) throws NoSuchPaddingException {
try {
- return Padding.valueOf(padding).ordinal();
+ return Padding.valueOf(padding);
} catch (final Exception e) {
throw new NoSuchPaddingException("Algorithm not supported: " + padding);
}
}
+
}
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/crypto/utils/Transformation.java b/src/main/java/org/apache/commons/crypto/utils/Transformation.java
index acef502..53a61b9 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Transformation.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Transformation.java
@@ -20,6 +20,8 @@ package org.apache.commons.crypto.utils;
import java.security.NoSuchAlgorithmException;
+import javax.crypto.NoSuchPaddingException;
+
/**
* Transformation algorithm, mode and padding, in the format "Algorithm/Mode/Padding", for example "AES/CBC/NoPadding".
*
@@ -27,14 +29,18 @@ import java.security.NoSuchAlgorithmException;
*/
public class Transformation {
+ private static final int T_DELIM_PARTS = 3;
+ private static final String T_DELIM_REGEX = "/";
+
/**
* Parses a transformation.
*
* @param transformation current transformation
* @return the Transformation
* @throws NoSuchAlgorithmException if the algorithm is not supported
+ * @throws NoSuchPaddingException Thrown when the padding is unsupported.
*/
- public static Transformation parse(final String transformation) throws NoSuchAlgorithmException {
+ public static Transformation parse(final String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
if (transformation == null) {
throw new NoSuchAlgorithmException("No transformation given.");
}
@@ -44,16 +50,16 @@ public class Transformation {
// algorithm (e.g., AES) index 1: mode (e.g., CTR) index 2: padding (e.g.,
// NoPadding)
//
- final String[] parts = transformation.split("/", 4);
- if (parts.length != 3) {
+ final String[] parts = transformation.split(T_DELIM_REGEX, T_DELIM_PARTS + 1);
+ if (parts.length != T_DELIM_PARTS) {
throw new NoSuchAlgorithmException("Invalid transformation format: " + transformation);
}
return new Transformation(parts[0], parts[1], parts[2]);
}
- final String algorithm;
- final String mode;
- final String padding;
+ private final String algorithm;
+ private final String mode;
+ private final Padding padding;
/**
* Constructs a new instance.
@@ -62,12 +68,24 @@ public class Transformation {
* @param mode the mode name
* @param padding the padding name
*/
- private Transformation(final String algorithm, final String mode, final String padding) {
+ private Transformation(final String algorithm, final String mode, final Padding padding) {
this.algorithm = algorithm;
this.mode = mode;
this.padding = padding;
}
+ /**
+ * Constructs a new instance.
+ *
+ * @param algorithm the algorithm name
+ * @param mode the mode name
+ * @param padding the padding name
+ * @throws NoSuchPaddingException Thrown when the padding is unsupported.
+ */
+ private Transformation(final String algorithm, final String mode, final String padding) throws NoSuchPaddingException {
+ this(algorithm, mode, Padding.get(padding));
+ }
+
/**
* Gets the algorithm.
*
@@ -91,7 +109,7 @@ public class Transformation {
*
* @return the padding.
*/
- public String getPadding() {
+ public Padding getPadding() {
return padding;
}
}
\ No newline at end of file