This is an automated email from the ASF dual-hosted git repository.
linghengqian 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 f7f7d83ab31 use common codec to encode hex (#28174)
f7f7d83ab31 is described below
commit f7f7d83ab313a8f2383d4b2f2ecb564fb983146b
Author: LiZongbo <[email protected]>
AuthorDate: Wed Aug 23 08:06:24 2023 +0800
use common codec to encode hex (#28174)
---
features/encrypt/plugin/sm/pom.xml | 6 ++++++
.../encrypt/sm/algorithm/SM3EncryptAlgorithm.java | 4 ++--
.../encrypt/sm/algorithm/SM4EncryptAlgorithm.java | 19 ++++++++++++++-----
3 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/features/encrypt/plugin/sm/pom.xml
b/features/encrypt/plugin/sm/pom.xml
index ae0ecfe9e2f..b4c4f751c78 100644
--- a/features/encrypt/plugin/sm/pom.xml
+++ b/features/encrypt/plugin/sm/pom.xml
@@ -51,5 +51,11 @@
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>${commons-codec.version}</version>
+ </dependency>
</dependencies>
</project>
diff --git
a/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM3EncryptAlgorithm.java
b/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM3EncryptAlgorithm.java
index c0d0b7065c8..33317c0027c 100644
---
a/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM3EncryptAlgorithm.java
+++
b/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM3EncryptAlgorithm.java
@@ -18,13 +18,13 @@
package org.apache.shardingsphere.encrypt.sm.algorithm;
import lombok.EqualsAndHashCode;
+import org.apache.commons.codec.binary.Hex;
import
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
import
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import java.nio.charset.StandardCharsets;
import java.security.Security;
@@ -60,7 +60,7 @@ public final class SM3EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
@Override
public String encrypt(final Object plainValue, final EncryptContext
encryptContext) {
- return null == plainValue ? null :
ByteUtils.toHexString(digest(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8),
sm3Salt));
+ return null == plainValue ? null :
Hex.encodeHexString(digest(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8),
sm3Salt));
}
@Override
diff --git
a/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithm.java
b/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithm.java
index 735bf795174..6653d76c6fe 100644
---
a/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithm.java
+++
b/features/encrypt/plugin/sm/src/main/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithm.java
@@ -19,12 +19,13 @@ package org.apache.shardingsphere.encrypt.sm.algorithm;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
import
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
import
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
@@ -87,7 +88,7 @@ public final class SM4EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
private byte[] createSm4Key(final Properties props) {
ShardingSpherePreconditions.checkState(props.containsKey(SM4_KEY), ()
-> new EncryptAlgorithmInitializationException("SM4", String.format("%s can not
be null", SM4_KEY)));
- byte[] result =
ByteUtils.fromHexString(String.valueOf(props.getProperty(SM4_KEY)));
+ byte[] result =
fromHexString(String.valueOf(props.getProperty(SM4_KEY)));
ShardingSpherePreconditions.checkState(KEY_LENGTH == result.length,
() -> new EncryptAlgorithmInitializationException("SM4", "Key
length must be " + KEY_LENGTH + " bytes long"));
return result;
@@ -99,7 +100,7 @@ public final class SM4EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
}
ShardingSpherePreconditions.checkState(props.containsKey(SM4_IV), ()
-> new EncryptAlgorithmInitializationException("SM4", String.format("%s can not
be null", SM4_IV)));
String sm4IvValue = String.valueOf(props.getProperty(SM4_IV));
- byte[] result = ByteUtils.fromHexString(sm4IvValue);
+ byte[] result = fromHexString(sm4IvValue);
ShardingSpherePreconditions.checkState(IV_LENGTH == result.length, ()
-> new EncryptAlgorithmInitializationException("SM4", "Iv length must be " +
IV_LENGTH + " bytes long"));
return result;
}
@@ -113,7 +114,7 @@ public final class SM4EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
@Override
public String encrypt(final Object plainValue, final EncryptContext
encryptContext) {
- return null == plainValue ? null :
ByteUtils.toHexString(encrypt(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8)));
+ return null == plainValue ? null :
Hex.encodeHexString(encrypt(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8)));
}
private byte[] encrypt(final byte[] plainValue) {
@@ -122,7 +123,7 @@ public final class SM4EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
@Override
public Object decrypt(final String cipherValue, final EncryptContext
encryptContext) {
- return null == cipherValue ? null : new
String(decrypt(ByteUtils.fromHexString(cipherValue)), StandardCharsets.UTF_8);
+ return null == cipherValue ? null : new
String(decrypt(fromHexString(cipherValue)), StandardCharsets.UTF_8);
}
private byte[] decrypt(final byte[] cipherValue) {
@@ -145,4 +146,12 @@ public final class SM4EncryptAlgorithm implements
StandardEncryptAlgorithm<Objec
public String getType() {
return "SM4";
}
+
+ static byte[] fromHexString(final String s) {
+ try {
+ return Hex.decodeHex(s);
+ } catch (DecoderException e) {
+ throw new EncryptAlgorithmInitializationException("SM",
e.getMessage());
+ }
+ }
}