This is an automated email from the ASF dual-hosted git repository.
iamhucong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 78acf8103f2 Minor refactor in EncryptAlgorithmEngine and
MessageDigestAlgorithmEngine (#39694)
78acf8103f2 is described below
commit 78acf8103f2a50ce85bc44660171264b53b43b60
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Sep 2 16:41:32 2026 +0800
Minor refactor in EncryptAlgorithmEngine and MessageDigestAlgorithmEngine
(#39694)
* Minor refactor in EncryptAlgorithmEngine and MessageDigestAlgorithmEngine
* Fix EncryptAlgorithmEngineTest invalid hex case
---
.../algorithm/engine/EncryptAlgorithmEngine.java | 22 ++++++-------------
.../engine/EncryptAlgorithmEngineTest.java | 25 ++++++++++++++++++++--
infra/algorithm/type/message-digest/core/pom.xml | 4 ++++
.../core/MessageDigestAlgorithmEngine.java | 10 ++-------
.../core/MessageDigestAlgorithmEngineTest.java | 4 ++--
.../messagedigest/md5/MD5MessageDigestEngine.java | 3 +--
6 files changed, 39 insertions(+), 29 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngine.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngine.java
index cc3df5e1fab..41c442b4be2 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngine.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngine.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.encrypt.algorithm.engine;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import
org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
@@ -39,10 +41,6 @@ public final class EncryptAlgorithmEngine {
private static final String ENCODER_SUFFIX = "-encoder";
- private static final char[] LOWER_HEX_DIGITS =
"0123456789abcdef".toCharArray();
-
- private static final char[] UPPER_HEX_DIGITS =
"0123456789ABCDEF".toCharArray();
-
/**
* Encrypt.
*
@@ -130,20 +128,14 @@ public final class EncryptAlgorithmEngine {
}
private static String encodeHex(final byte[] value, final boolean
lowerCase) {
- char[] digits = lowerCase ? LOWER_HEX_DIGITS : UPPER_HEX_DIGITS;
- StringBuilder result = new StringBuilder(value.length * 2);
- for (byte each : value) {
- result.append(digits[each >> 4 & 0xF]);
- result.append(digits[each & 0xF]);
- }
- return result.toString();
+ return Hex.encodeHexString(value, lowerCase);
}
private static byte[] decodeHex(final String value) {
- byte[] result = new byte[value.length() / 2];
- for (int i = 0; i < value.length(); i += 2) {
- result[i / 2] = (byte) Integer.parseInt(value.substring(i, i + 2),
16);
+ try {
+ return Hex.decodeHex(value);
+ } catch (final DecoderException ex) {
+ throw new IllegalArgumentException(ex);
}
- return result;
}
}
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngineTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngineTest.java
index 32e4da4ac02..793bfac1105 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngineTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/engine/EncryptAlgorithmEngineTest.java
@@ -22,6 +22,8 @@ import
org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import
org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.nio.charset.StandardCharsets;
@@ -30,6 +32,7 @@ import java.util.Properties;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -49,9 +52,9 @@ class EncryptAlgorithmEngineTest {
@Test
void assertEncryptWithHexEncoder() {
EncryptAlgorithm encryptAlgorithm = mockEncryptAlgorithm(byte[].class);
- when(encryptAlgorithm.encrypt(eq("plain"),
any(AlgorithmSQLContext.class))).thenReturn("cipher".getBytes(StandardCharsets.UTF_8));
+ when(encryptAlgorithm.encrypt(eq("plain"),
any(AlgorithmSQLContext.class))).thenReturn(new byte[]{0, 15, 16, (byte) 0xFF});
Object actualCipherValue =
EncryptAlgorithmEngine.encrypt(encryptAlgorithm, "plain",
createAlgorithmSQLContext(), "HEX", false);
- assertThat(actualCipherValue, is("636970686572"));
+ assertThat(actualCipherValue, is("000F10FF"));
}
@Test
@@ -100,6 +103,24 @@ class EncryptAlgorithmEngineTest {
assertThat(actualPlainValue, is("plain"));
}
+ @Test
+ void assertDecryptWithHexEncoder() {
+ EncryptAlgorithm encryptAlgorithm = mockEncryptAlgorithm(byte[].class);
+ when(encryptAlgorithm.decrypt(any(byte[].class),
any(AlgorithmSQLContext.class))).thenReturn("plain");
+ Object actualPlainValue =
EncryptAlgorithmEngine.decrypt(encryptAlgorithm, "000f10Ff",
createAlgorithmSQLContext(), "HEX");
+ ArgumentCaptor<byte[]> actualCipherValueCaptor =
ArgumentCaptor.forClass(byte[].class);
+ verify(encryptAlgorithm).decrypt(actualCipherValueCaptor.capture(),
any(AlgorithmSQLContext.class));
+ assertThat(actualCipherValueCaptor.getValue(), is(new byte[]{0, 15,
16, (byte) 0xFF}));
+ assertThat(actualPlainValue, is("plain"));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = {"0", "GG"})
+ void assertDecryptWithInvalidHexEncoder(final String cipherValue) {
+ EncryptAlgorithm encryptAlgorithm = mockEncryptAlgorithm(byte[].class);
+ assertThrows(IllegalArgumentException.class, () ->
EncryptAlgorithmEngine.decrypt(encryptAlgorithm, cipherValue,
createAlgorithmSQLContext(), "HEX"));
+ }
+
@Test
void assertDecryptWithoutEncoder() {
EncryptAlgorithm encryptAlgorithm = mockEncryptAlgorithm(byte[].class);
diff --git a/infra/algorithm/type/message-digest/core/pom.xml
b/infra/algorithm/type/message-digest/core/pom.xml
index 1e61325f41f..5d57d83fd0a 100644
--- a/infra/algorithm/type/message-digest/core/pom.xml
+++ b/infra/algorithm/type/message-digest/core/pom.xml
@@ -32,5 +32,9 @@
<artifactId>shardingsphere-infra-algorithm-message-digest-spi</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git
a/infra/algorithm/type/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngine.java
b/infra/algorithm/type/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngine.java
index d183f040596..93f19b946c8 100644
---
a/infra/algorithm/type/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngine.java
+++
b/infra/algorithm/type/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngine.java
@@ -19,6 +19,7 @@ package
org.apache.shardingsphere.infra.algorithm.messagedigest.core;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.commons.codec.binary.Hex;
import
org.apache.shardingsphere.infra.algorithm.messagedigest.spi.MessageDigestAlgorithm;
import java.util.Base64;
@@ -31,8 +32,6 @@ public final class MessageDigestAlgorithmEngine {
private static final String BASE64 = "BASE64";
- private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
-
/**
* Digest.
*
@@ -63,11 +62,6 @@ public final class MessageDigestAlgorithmEngine {
}
private static String encodeHex(final byte[] value) {
- StringBuilder result = new StringBuilder(value.length * 2);
- for (byte each : value) {
- result.append(HEX_DIGITS[each >> 4 & 0xF]);
- result.append(HEX_DIGITS[each & 0xF]);
- }
- return result.toString();
+ return Hex.encodeHexString(value);
}
}
diff --git
a/infra/algorithm/type/message-digest/core/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngineTest.java
b/infra/algorithm/type/message-digest/core/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngineTest.java
index 1d126b869c1..273caa801c4 100644
---
a/infra/algorithm/type/message-digest/core/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngineTest.java
+++
b/infra/algorithm/type/message-digest/core/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithmEngineTest.java
@@ -43,9 +43,9 @@ class MessageDigestAlgorithmEngineTest {
@Test
void assertDigestWithHexEncoder() {
MessageDigestAlgorithm digestAlgorithm =
mock(MessageDigestAlgorithm.class);
-
when(digestAlgorithm.digest("plain")).thenReturn("digest".getBytes(StandardCharsets.UTF_8));
+ when(digestAlgorithm.digest("plain")).thenReturn(new byte[]{0, 15, 16,
(byte) 0xFF});
Object actualDigestValue =
MessageDigestAlgorithmEngine.digest(digestAlgorithm, "plain", "HEX", false);
- assertThat(actualDigestValue, is("646967657374"));
+ assertThat(actualDigestValue, is("000f10ff"));
}
@Test
diff --git
a/infra/algorithm/type/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestEngine.java
b/infra/algorithm/type/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestEngine.java
index c907408f5e7..f4dec0218f0 100644
---
a/infra/algorithm/type/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestEngine.java
+++
b/infra/algorithm/type/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestEngine.java
@@ -21,7 +21,6 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.algorithm.core.util.AlgorithmValueUtils;
-import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Properties;
@@ -57,6 +56,6 @@ public final class MD5MessageDigestEngine {
* @throws GeneralSecurityException digest failure
*/
public static byte[] digest(final Object plainValue, final String salt)
throws GeneralSecurityException {
- return
MessageDigest.getInstance(MD5).digest((AlgorithmValueUtils.convertToUTF8Text(plainValue)
+ salt).getBytes(StandardCharsets.UTF_8));
+ return
MessageDigest.getInstance(MD5).digest(AlgorithmValueUtils.convertToBytes(AlgorithmValueUtils.convertToUTF8Text(plainValue)
+ salt, null));
}
}