This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 ed7d9e1a65f Optimize error code for encrypt. (#23339)
ed7d9e1a65f is described below

commit ed7d9e1a65f524b69114660ffd38e9e78f6afca4
Author: Chuxin Chen <[email protected]>
AuthorDate: Thu Jan 5 15:26:41 2023 +0800

    Optimize error code for encrypt. (#23339)
    
    * Optimize error code for encrypt.
    
    * Optimize error code for encrypt.
---
 .../algorithm/encrypt/AESEncryptAlgorithm.java       |  5 +++--
 .../algorithm/encrypt/AESEncryptAlgorithmTest.java   |  3 ++-
 features/encrypt/plugin/sm/pom.xml                   |  6 ++++++
 .../encrypt/sm/algorithm/SM3EncryptAlgorithm.java    |  6 ++++--
 .../encrypt/sm/algorithm/SM4EncryptAlgorithm.java    | 20 +++++++++++---------
 .../sm/algorithm/SM4EncryptAlgorithmTest.java        |  3 ++-
 6 files changed, 28 insertions(+), 15 deletions(-)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java
index 5d9fa179e43..379e9768db2 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java
@@ -17,12 +17,13 @@
 
 package org.apache.shardingsphere.encrypt.algorithm.encrypt;
 
-import com.google.common.base.Preconditions;
 import lombok.Getter;
 import lombok.SneakyThrows;
 import org.apache.commons.codec.digest.DigestUtils;
 import 
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
+import 
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import javax.crypto.Cipher;
 import javax.crypto.NoSuchPaddingException;
@@ -54,7 +55,7 @@ public final class AESEncryptAlgorithm implements 
StandardEncryptAlgorithm<Objec
     }
     
     private byte[] createSecretKey(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(AES_KEY), "%s can not be 
null.", AES_KEY);
+        ShardingSpherePreconditions.checkState(props.containsKey(AES_KEY), () 
-> new EncryptAlgorithmInitializationException("AES", String.format("%s can not 
be null.", AES_KEY)));
         return Arrays.copyOf(DigestUtils.sha1(props.getProperty(AES_KEY)), 16);
     }
     
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java
index a2571f6f475..ceb50b28f4f 100644
--- 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.encrypt.algorithm.encrypt;
 
 import 
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
+import 
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
 import 
org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithmFactory;
@@ -43,7 +44,7 @@ public final class AESEncryptAlgorithmTest {
         encryptAlgorithm = ShardingSphereAlgorithmFactory.createAlgorithm(new 
AlgorithmConfiguration("AES", PropertiesBuilder.build(new 
Property("aes-key-value", "test"))), EncryptAlgorithm.class);
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = EncryptAlgorithmInitializationException.class)
     public void assertCreateNewInstanceWithoutAESKey() {
         ShardingSphereAlgorithmFactory.createAlgorithm(new 
AlgorithmConfiguration("AES", new Properties()), EncryptAlgorithm.class);
     }
diff --git a/features/encrypt/plugin/sm/pom.xml 
b/features/encrypt/plugin/sm/pom.xml
index f883ce80cdf..49afc658cb6 100644
--- a/features/encrypt/plugin/sm/pom.xml
+++ b/features/encrypt/plugin/sm/pom.xml
@@ -38,6 +38,12 @@
             <version>${project.version}</version>
         </dependency>
         
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-encrypt-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-test-util</artifactId>
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 852c04c687d..d5b06697e85 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
@@ -17,10 +17,11 @@
 
 package org.apache.shardingsphere.encrypt.sm.algorithm;
 
-import com.google.common.base.Preconditions;
 import lombok.Getter;
 import 
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
+import 
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.bouncycastle.crypto.digests.SM3Digest;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
@@ -55,7 +56,8 @@ public final class SM3EncryptAlgorithm implements 
StandardEncryptAlgorithm<Objec
     
     private byte[] createSm3Salt(final Properties props) {
         String salt = null == props.getProperty(SM3_SALT) ? "" : 
String.valueOf(props.getProperty(SM3_SALT));
-        Preconditions.checkState(salt.isEmpty() || SALT_LENGTH == 
salt.length(), "Salt should be either blank or better " + SALT_LENGTH + " bytes 
long.");
+        ShardingSpherePreconditions.checkState(salt.isEmpty() || SALT_LENGTH 
== salt.length(),
+                () -> new EncryptAlgorithmInitializationException("SM3", "Salt 
should be either blank or better " + SALT_LENGTH + " bytes long."));
         return salt.isEmpty() ? new byte[0] : 
salt.getBytes(StandardCharsets.UTF_8);
     }
     
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 dd397152620..3ef4e32a58b 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
@@ -17,11 +17,12 @@
 
 package org.apache.shardingsphere.encrypt.sm.algorithm;
 
-import com.google.common.base.Preconditions;
 import lombok.Getter;
 import lombok.SneakyThrows;
 import 
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
+import 
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
 
@@ -82,16 +83,17 @@ public final class SM4EncryptAlgorithm implements 
StandardEncryptAlgorithm<Objec
     }
     
     private String createSm4Mode(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(SM4_MODE), "%s can not 
be null.", SM4_MODE);
+        ShardingSpherePreconditions.checkState(props.containsKey(SM4_MODE), () 
-> new EncryptAlgorithmInitializationException("SM4", String.format("%s can not 
be null.", SM4_MODE)));
         String result = 
String.valueOf(props.getProperty(SM4_MODE)).toUpperCase();
-        Preconditions.checkState(MODES.contains(result), "Mode must be either 
CBC or ECB.");
+        ShardingSpherePreconditions.checkState(MODES.contains(result), () -> 
new EncryptAlgorithmInitializationException("SM4", "Mode must be either CBC or 
ECB."));
         return result;
     }
     
     private byte[] createSm4Key(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(SM4_KEY), "%s can not be 
null.", SM4_KEY);
+        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)));
-        Preconditions.checkState(KEY_LENGTH == result.length, "Key length must 
be " + KEY_LENGTH + " bytes long.");
+        ShardingSpherePreconditions.checkState(KEY_LENGTH == result.length,
+                () -> new EncryptAlgorithmInitializationException("SM4", "Key 
length must be " + KEY_LENGTH + " bytes long."));
         return result;
     }
     
@@ -99,17 +101,17 @@ public final class SM4EncryptAlgorithm implements 
StandardEncryptAlgorithm<Objec
         if (!"CBC".equalsIgnoreCase(sm4Mode)) {
             return null;
         }
-        Preconditions.checkArgument(props.containsKey(SM4_IV), "%s can not be 
null.", SM4_IV);
+        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);
-        Preconditions.checkState(IV_LENGTH == result.length, "Iv length must 
be " + IV_LENGTH + " bytes long.");
+        ShardingSpherePreconditions.checkState(IV_LENGTH == result.length, () 
-> new EncryptAlgorithmInitializationException("SM4", "Iv length must be " + 
IV_LENGTH + " bytes long."));
         return result;
     }
     
     private String createSm4Padding(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(SM4_PADDING), "%s can 
not be null.", SM4_PADDING);
+        ShardingSpherePreconditions.checkState(props.containsKey(SM4_PADDING), 
() -> new EncryptAlgorithmInitializationException("SM4", String.format("%s can 
not be null.", SM4_PADDING)));
         String result = 
String.valueOf(props.getProperty(SM4_PADDING)).toUpperCase().replace("PADDING", 
"Padding");
-        Preconditions.checkState(PADDINGS.contains(result), "Padding must be 
either PKCS5Padding or PKCS7Padding.");
+        ShardingSpherePreconditions.checkState(PADDINGS.contains(result), () 
-> new EncryptAlgorithmInitializationException("SM4", "Padding must be either 
PKCS5Padding or PKCS7Padding."));
         return result;
     }
     
diff --git 
a/features/encrypt/plugin/sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
 
b/features/encrypt/plugin/sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
index 32d9fe3d5bb..75687dd6572 100644
--- 
a/features/encrypt/plugin/sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
+++ 
b/features/encrypt/plugin/sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.encrypt.sm.algorithm;
 
 import 
org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm;
+import 
org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
 import 
org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithmFactory;
@@ -35,7 +36,7 @@ import static org.mockito.Mockito.mock;
 
 public final class SM4EncryptAlgorithmTest {
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = EncryptAlgorithmInitializationException.class)
     public void assertInitWithoutKey() {
         StandardEncryptAlgorithm<Object, String> algorithm = 
ShardingSphereAlgorithmFactory.createAlgorithm(new 
AlgorithmConfiguration("SM4", createECBProperties()), EncryptAlgorithm.class);
         algorithm.init(PropertiesBuilder.build(new Property("sm4-mode", 
"ECB"), new Property("sm4-padding", "PKCS5Padding")));

Reply via email to