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

zhangliang 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 662d46015a2 Add EncryptAlgorithm.toConfiguration() to instead of 
equals (#32272)
662d46015a2 is described below

commit 662d46015a252bcc21bdcd3f3a98301be29db9e0
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jul 26 00:11:28 2024 +0800

    Add EncryptAlgorithm.toConfiguration() to instead of equals (#32272)
    
    * Add EncryptAlgorithm.toConfiguration() to instead of equals
    
    * Add EncryptAlgorithm.toConfiguration() to instead of equals
---
 .../apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java  | 12 ++++++++++++
 .../algorithm/assisted/MD5AssistedEncryptAlgorithm.java      | 11 +++++++++--
 .../encrypt/algorithm/standard/AESEncryptAlgorithm.java      | 11 +++++++++--
 .../rewrite/token/comparator/EncryptorComparator.java        |  8 +-------
 .../infra/algorithm/core/config/AlgorithmConfiguration.java  |  2 ++
 5 files changed, 33 insertions(+), 11 deletions(-)

diff --git 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
index beffbfb3259..573880131aa 100644
--- 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
+++ 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
@@ -18,8 +18,11 @@
 package org.apache.shardingsphere.encrypt.spi;
 
 import org.apache.shardingsphere.infra.algorithm.core.ShardingSphereAlgorithm;
+import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
 
+import java.util.Properties;
+
 /**
  * Encrypt algorithm.
  */
@@ -49,4 +52,13 @@ public interface EncryptAlgorithm extends 
ShardingSphereAlgorithm {
      * @return encrypt algorithm meta data
      */
     EncryptAlgorithmMetaData getMetaData();
+    
+    /**
+     * convert to encryptor configuration.
+     *
+     * @return converted configuration
+     */
+    default AlgorithmConfiguration toConfiguration() {
+        return new AlgorithmConfiguration(getType(), new Properties());
+    }
 }
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 1a9d9c28279..27ea588c461 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
@@ -17,10 +17,10 @@
 
 package org.apache.shardingsphere.encrypt.algorithm.assisted;
 
-import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
 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.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -30,16 +30,18 @@ import java.util.Properties;
 /**
  * MD5 assisted encrypt algorithm.
  */
-@EqualsAndHashCode
 public final class MD5AssistedEncryptAlgorithm implements EncryptAlgorithm {
     
     @Getter
     private final EncryptAlgorithmMetaData metaData = new 
EncryptAlgorithmMetaData(false, true, false);
     
+    private Properties props;
+    
     private MessageDigestAlgorithm digestAlgorithm;
     
     @Override
     public void init(final Properties props) {
+        this.props = props;
         digestAlgorithm = 
TypedSPILoader.getService(MessageDigestAlgorithm.class, getType(), props);
     }
     
@@ -53,6 +55,11 @@ public final class MD5AssistedEncryptAlgorithm implements 
EncryptAlgorithm {
         throw new UnsupportedOperationException(String.format("Algorithm `%s` 
is unsupported to decrypt", getType()));
     }
     
+    @Override
+    public AlgorithmConfiguration toConfiguration() {
+        return new AlgorithmConfiguration(getType(), props);
+    }
+    
     @Override
     public String getType() {
         return "MD5";
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 228c22b8620..fef41a0a4af 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
@@ -17,12 +17,12 @@
 
 package org.apache.shardingsphere.encrypt.algorithm.standard;
 
-import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.SneakyThrows;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
 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.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
@@ -38,7 +38,6 @@ import java.util.Properties;
 /**
  * AES encrypt algorithm.
  */
-@EqualsAndHashCode
 public final class AESEncryptAlgorithm implements EncryptAlgorithm {
     
     private static final String AES_KEY = "aes-key-value";
@@ -48,10 +47,13 @@ public final class AESEncryptAlgorithm implements 
EncryptAlgorithm {
     @Getter
     private final EncryptAlgorithmMetaData metaData = new 
EncryptAlgorithmMetaData(true, true, false);
     
+    private Properties props;
+    
     private byte[] secretKey;
     
     @Override
     public void init(final Properties props) {
+        this.props = props;
         secretKey = getSecretKey(props);
     }
     
@@ -89,6 +91,11 @@ public final class AESEncryptAlgorithm implements 
EncryptAlgorithm {
         return result;
     }
     
+    @Override
+    public AlgorithmConfiguration toConfiguration() {
+        return new AlgorithmConfiguration(getType(), props);
+    }
+    
     @Override
     public String getType() {
         return "AES";
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/comparator/EncryptorComparator.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/comparator/EncryptorComparator.java
index 2dd7474d9c6..db393aec8ef 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/comparator/EncryptorComparator.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/comparator/EncryptorComparator.java
@@ -35,12 +35,6 @@ public final class EncryptorComparator {
      * @return same encryptors or not
      */
     public static boolean isSame(final EncryptAlgorithm encryptor1, final 
EncryptAlgorithm encryptor2) {
-        if (null == encryptor1 && null == encryptor2) {
-            return true;
-        }
-        if (null != encryptor1 && null != encryptor2) {
-            return encryptor1.getType().equals(encryptor2.getType()) && 
encryptor1.equals(encryptor2);
-        }
-        return false;
+        return null != encryptor1 && null != encryptor2 ? 
encryptor1.toConfiguration().equals(encryptor2.toConfiguration()) : encryptor1 
== encryptor2;
     }
 }
diff --git 
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/config/AlgorithmConfiguration.java
 
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/config/AlgorithmConfiguration.java
index 3d2a2826149..333293bf53d 100644
--- 
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/config/AlgorithmConfiguration.java
+++ 
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/config/AlgorithmConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.algorithm.core.config;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import lombok.EqualsAndHashCode;
 import lombok.Getter;
 
 import java.util.Properties;
@@ -27,6 +28,7 @@ import java.util.Properties;
  * Algorithm configuration.
  */
 @Getter
+@EqualsAndHashCode
 public final class AlgorithmConfiguration {
     
     private final String type;

Reply via email to