This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 7cfd1df3092 Add new message-digest module and new
MD5MessageDigestAlgorithm (#30650)
7cfd1df3092 is described below
commit 7cfd1df30921b3002c7ac87f47a8222105b2329b
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Mar 26 13:37:58 2024 +0800
Add new message-digest module and new MD5MessageDigestAlgorithm (#30650)
---
infra/algorithm/{ => message-digest/core}/pom.xml | 17 +++----
.../messagedigest/core/MessageDigestAlgorithm.java | 34 +++++++++++++
infra/algorithm/{ => message-digest}/pom.xml | 7 ++-
.../{ => message-digest/type/md5}/pom.xml | 22 +++++----
.../md5/MD5MessageDigestAlgorithm.java | 48 +++++++++++++++++++
...rithm.messagedigest.core.MessageDigestAlgorithm | 18 +++++++
.../md5/MD5MessageDigestAlgorithmTest.java | 55 ++++++++++++++++++++++
infra/algorithm/{ => message-digest/type}/pom.xml | 8 ++--
infra/algorithm/pom.xml | 1 +
9 files changed, 185 insertions(+), 25 deletions(-)
diff --git a/infra/algorithm/pom.xml
b/infra/algorithm/message-digest/core/pom.xml
similarity index 75%
copy from infra/algorithm/pom.xml
copy to infra/algorithm/message-digest/core/pom.xml
index c73625e760c..776560b7f8e 100644
--- a/infra/algorithm/pom.xml
+++ b/infra/algorithm/message-digest/core/pom.xml
@@ -20,16 +20,17 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-infra</artifactId>
+ <artifactId>shardingsphere-infra-algorithm-message-digest</artifactId>
<version>5.4.2-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-infra-algorithm</artifactId>
- <packaging>pom</packaging>
+ <artifactId>shardingsphere-infra-algorithm-message-digest-core</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>core</module>
- <module>key-generator</module>
- <module>load-balancer</module>
- </modules>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-infra-algorithm-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
</project>
diff --git
a/infra/algorithm/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithm.java
b/infra/algorithm/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithm.java
new file mode 100644
index 00000000000..989cbeba0d2
--- /dev/null
+++
b/infra/algorithm/message-digest/core/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/core/MessageDigestAlgorithm.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.algorithm.messagedigest.core;
+
+import org.apache.shardingsphere.infra.algorithm.core.ShardingSphereAlgorithm;
+
+/**
+ * Message digest algorithm.
+ */
+public interface MessageDigestAlgorithm extends ShardingSphereAlgorithm {
+
+ /**
+ * Digest.
+ *
+ * @param plainValue plain value
+ * @return digest value
+ */
+ String digest(Object plainValue);
+}
diff --git a/infra/algorithm/pom.xml b/infra/algorithm/message-digest/pom.xml
similarity index 87%
copy from infra/algorithm/pom.xml
copy to infra/algorithm/message-digest/pom.xml
index c73625e760c..22464e9e327 100644
--- a/infra/algorithm/pom.xml
+++ b/infra/algorithm/message-digest/pom.xml
@@ -20,16 +20,15 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-infra</artifactId>
+ <artifactId>shardingsphere-infra-algorithm</artifactId>
<version>5.4.2-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-infra-algorithm</artifactId>
+ <artifactId>shardingsphere-infra-algorithm-message-digest</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<modules>
<module>core</module>
- <module>key-generator</module>
- <module>load-balancer</module>
+ <module>type</module>
</modules>
</project>
diff --git a/infra/algorithm/pom.xml
b/infra/algorithm/message-digest/type/md5/pom.xml
similarity index 68%
copy from infra/algorithm/pom.xml
copy to infra/algorithm/message-digest/type/md5/pom.xml
index c73625e760c..d942ed97f81 100644
--- a/infra/algorithm/pom.xml
+++ b/infra/algorithm/message-digest/type/md5/pom.xml
@@ -20,16 +20,22 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-infra</artifactId>
+
<artifactId>shardingsphere-infra-algorithm-message-digest-type</artifactId>
<version>5.4.2-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-infra-algorithm</artifactId>
- <packaging>pom</packaging>
+ <artifactId>shardingsphere-infra-algorithm-message-digest-md5</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>core</module>
- <module>key-generator</module>
- <module>load-balancer</module>
- </modules>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-infra-algorithm-message-digest-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
+ </dependencies>
</project>
diff --git
a/infra/algorithm/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithm.java
b/infra/algorithm/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithm.java
new file mode 100644
index 00000000000..af1c5f31cdf
--- /dev/null
+++
b/infra/algorithm/message-digest/type/md5/src/main/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithm.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.algorithm.messagedigest.md5;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import
org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * MD5 message digest algorithm.
+ */
+public final class MD5MessageDigestAlgorithm implements MessageDigestAlgorithm
{
+
+ private static final String SALT_KEY = "salt";
+
+ private String salt;
+
+ @Override
+ public void init(final Properties props) {
+ salt = props.getProperty(SALT_KEY, "");
+ }
+
+ @Override
+ public String digest(final Object plainValue) {
+ return null == plainValue ? null : DigestUtils.md5Hex(plainValue +
salt);
+ }
+
+ @Override
+ public String getType() {
+ return "MD5";
+ }
+}
diff --git
a/infra/algorithm/message-digest/type/md5/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm
b/infra/algorithm/message-digest/type/md5/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm
new file mode 100644
index 00000000000..26570a727c4
--- /dev/null
+++
b/infra/algorithm/message-digest/type/md5/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.infra.algorithm.messagedigest.md5.MD5MessageDigestAlgorithm
diff --git
a/infra/algorithm/message-digest/type/md5/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithmTest.java
b/infra/algorithm/message-digest/type/md5/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithmTest.java
new file mode 100644
index 00000000000..865c6022d29
--- /dev/null
+++
b/infra/algorithm/message-digest/type/md5/src/test/java/org/apache/shardingsphere/infra/algorithm/messagedigest/md5/MD5MessageDigestAlgorithmTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.algorithm.messagedigest.md5;
+
+import
org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class MD5MessageDigestAlgorithmTest {
+
+ private MessageDigestAlgorithm digestAlgorithm;
+
+ @BeforeEach
+ void setUp() {
+ digestAlgorithm =
TypedSPILoader.getService(MessageDigestAlgorithm.class, "MD5");
+ }
+
+ @Test
+ void assertDigest() {
+ assertThat(digestAlgorithm.digest("test"),
is("098f6bcd4621d373cade4e832627b4f6"));
+ }
+
+ @Test
+ void assertDigestWithNullPlaintext() {
+ assertNull(digestAlgorithm.digest(null));
+ }
+
+ @Test
+ void assertDigestWhenConfigSalt() {
+ digestAlgorithm.init(PropertiesBuilder.build(new Property("salt",
"202cb962ac5907")));
+ assertThat(digestAlgorithm.digest("test"),
is("0c243d2934937738f36514035d95344a"));
+ }
+}
diff --git a/infra/algorithm/pom.xml
b/infra/algorithm/message-digest/type/pom.xml
similarity index 85%
copy from infra/algorithm/pom.xml
copy to infra/algorithm/message-digest/type/pom.xml
index c73625e760c..8d373f62341 100644
--- a/infra/algorithm/pom.xml
+++ b/infra/algorithm/message-digest/type/pom.xml
@@ -20,16 +20,14 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-infra</artifactId>
+ <artifactId>shardingsphere-infra-algorithm-message-digest</artifactId>
<version>5.4.2-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-infra-algorithm</artifactId>
+ <artifactId>shardingsphere-infra-algorithm-message-digest-type</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<modules>
- <module>core</module>
- <module>key-generator</module>
- <module>load-balancer</module>
+ <module>md5</module>
</modules>
</project>
diff --git a/infra/algorithm/pom.xml b/infra/algorithm/pom.xml
index c73625e760c..bf3853ce03d 100644
--- a/infra/algorithm/pom.xml
+++ b/infra/algorithm/pom.xml
@@ -31,5 +31,6 @@
<module>core</module>
<module>key-generator</module>
<module>load-balancer</module>
+ <module>message-digest</module>
</modules>
</project>