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

sunnianjun 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 01c043c632c Refactor EncryptRule (#30207)
01c043c632c is described below

commit 01c043c632cf584e95d89cc6ac4c6c6b1fd81570
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 20 14:39:02 2024 +0800

    Refactor EncryptRule (#30207)
---
 .../algorithm/assisted/MD5AssistedEncryptAlgorithm.java     |  6 +++++-
 .../encrypt/algorithm/standard/AESEncryptAlgorithm.java     |  7 +++----
 .../org/apache/shardingsphere/encrypt/rule/EncryptRule.java | 13 +++++++++++--
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java
index c03e2fa21fa..525aa87a35d 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java
@@ -41,7 +41,11 @@ public final class MD5AssistedEncryptAlgorithm implements 
EncryptAlgorithm {
     
     @Override
     public void init(final Properties props) {
-        salt = props.getProperty(SALT_KEY, "");
+        salt = getSalt(props);
+    }
+    
+    private String getSalt(final Properties props) {
+        return props.getProperty(SALT_KEY, "");
     }
     
     @Override
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
index e08643e7488..fa902c58c8b 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java
@@ -54,13 +54,12 @@ public final class AESEncryptAlgorithm implements 
EncryptAlgorithm {
     
     @Override
     public void init(final Properties props) {
-        secretKey = createSecretKey(props);
+        secretKey = getSecretKey(props);
     }
     
-    private byte[] createSecretKey(final Properties props) {
+    private byte[] getSecretKey(final Properties props) {
         String aesKey = props.getProperty(AES_KEY);
-        ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(aesKey),
-                () -> new EncryptAlgorithmInitializationException(getType(), 
String.format("%s can not be null or empty", AES_KEY)));
+        ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(aesKey), 
() -> new EncryptAlgorithmInitializationException(getType(), String.format("%s 
can not be null or empty", AES_KEY)));
         String digestAlgorithm = props.getProperty(DIGEST_ALGORITHM_NAME, 
MessageDigestAlgorithms.SHA_1);
         return 
Arrays.copyOf(DigestUtils.getDigest(digestAlgorithm.toUpperCase()).digest(aesKey.getBytes(StandardCharsets.UTF_8)),
 16);
     }
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 86e99a2d976..024633f28a2 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfigu
 import 
org.apache.shardingsphere.encrypt.exception.algorithm.MismatchedEncryptAlgorithmTypeException;
 import 
org.apache.shardingsphere.encrypt.exception.metadata.EncryptTableNotFoundException;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
 import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
@@ -32,6 +33,7 @@ import 
org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 
 /**
@@ -51,8 +53,7 @@ public final class EncryptRule implements DatabaseRule, 
TableContainedRule {
     public EncryptRule(final String databaseName, final 
EncryptRuleConfiguration ruleConfig) {
         this.databaseName = databaseName;
         configuration = ruleConfig;
-        Map<String, EncryptAlgorithm> encryptors = new LinkedHashMap<>();
-        ruleConfig.getEncryptors().forEach((key, value) -> encryptors.put(key, 
TypedSPILoader.getService(EncryptAlgorithm.class, value.getType(), 
value.getProps())));
+        Map<String, EncryptAlgorithm> encryptors = 
createEncryptors(ruleConfig);
         for (EncryptTableRuleConfiguration each : ruleConfig.getTables()) {
             each.getColumns().forEach(columnRuleConfig -> 
checkEncryptorType(columnRuleConfig, encryptors));
             tables.put(each.getName().toLowerCase(), new EncryptTable(each, 
encryptors));
@@ -60,6 +61,14 @@ public final class EncryptRule implements DatabaseRule, 
TableContainedRule {
         }
     }
     
+    private Map<String, EncryptAlgorithm> createEncryptors(final 
EncryptRuleConfiguration ruleConfig) {
+        Map<String, EncryptAlgorithm> result = new 
LinkedHashMap<>(ruleConfig.getEncryptors().size(), 1F);
+        for (Entry<String, AlgorithmConfiguration> entry : 
ruleConfig.getEncryptors().entrySet()) {
+            result.put(entry.getKey(), 
TypedSPILoader.getService(EncryptAlgorithm.class, entry.getValue().getType(), 
entry.getValue().getProps()));
+        }
+        return result;
+    }
+    
     private void checkEncryptorType(final EncryptColumnRuleConfiguration 
columnRuleConfig, final Map<String, EncryptAlgorithm> encryptors) {
         
ShardingSpherePreconditions.checkState(encryptors.containsKey(columnRuleConfig.getCipher().getEncryptorName())
                 && 
encryptors.get(columnRuleConfig.getCipher().getEncryptorName()).getMetaData().isSupportDecrypt(),

Reply via email to