You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/30 12:04:04 UTC
[shardingsphere] branch master updated: fix SM4 with CBC (#17226)
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 e105cb69a9a fix SM4 with CBC (#17226)
e105cb69a9a is described below
commit e105cb69a9a9e06efbd38e7b7983bc29f375927f
Author: cheese8 <yi...@163.com>
AuthorDate: Sat Apr 30 20:03:50 2022 +0800
fix SM4 with CBC (#17226)
---
.../encrypt/algorithm/SM4EncryptAlgorithm.java | 1 +
...hmTest.java => SM4EncryptAlgorithmCBCTest.java} | 25 ++++++++++++----------
...hmTest.java => SM4EncryptAlgorithmECBTest.java} | 12 +++++------
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
index c8c8a59bda0..8cbd94814ac 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
@@ -104,6 +104,7 @@ public final class SM4EncryptAlgorithm implements EncryptAlgorithm<Object, Strin
String sm4IvValue = String.valueOf(props.getProperty(SM4_IV));
byte[] sm4IvBytes = ByteUtils.fromHexString(sm4IvValue);
Preconditions.checkState(IV_LENGTH == sm4IvBytes.length, "Iv length must be " + IV_LENGTH + " bytes long.");
+ return sm4IvBytes;
}
return null;
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
similarity index 76%
copy from shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
copy to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
index 56f997e5e9b..2c07cefb5c0 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
@@ -31,31 +31,33 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
-public final class SM4EncryptAlgorithmTest {
+public final class SM4EncryptAlgorithmCBCTest {
private EncryptAlgorithm<Object, String> encryptAlgorithm;
@Before
public void setUp() {
Properties props = new Properties();
- props.setProperty("sm4-key", "4D744E003D713D054E7E407C350E447E");
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
+ props.setProperty("sm4-key", "f201326119911788cFd30575b81059ac");
+ props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
+ props.setProperty("sm4-mode", "CBC");
+ props.setProperty("sm4-padding", "PKCS7Padding");
encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SM4", props));
}
@Test
- public void assertEncryptWithECBAndPKCS5Padding() {
- assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
+ public void assertEncrypt() {
+ assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("dca2127b57ba8cac36a0914e0208dc11"));
}
@Test(expected = IllegalArgumentException.class)
public void assertEncryptWithoutKey() {
Properties props = new Properties();
+ props.setProperty("sm4-mode", "CBC");
+ props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
+ props.setProperty("sm4-padding", "PKCS7Padding");
encryptAlgorithm.setProps(props);
encryptAlgorithm.init();
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
}
@@ -66,16 +68,17 @@ public final class SM4EncryptAlgorithmTest {
@Test
public void assertDecrypt() {
- assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde", mock(EncryptContext.class)).toString(), is("test"));
+ assertThat(encryptAlgorithm.decrypt("dca2127b57ba8cac36a0914e0208dc11", mock(EncryptContext.class)).toString(), is("test"));
}
@Test(expected = IllegalArgumentException.class)
public void assertDecryptWithoutKey() {
Properties props = new Properties();
+ props.setProperty("sm4-mode", "CBC");
+ props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
+ props.setProperty("sm4-padding", "PKCS7Padding");
encryptAlgorithm.setProps(props);
encryptAlgorithm.init();
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde", mock(EncryptContext.class)).toString(), is("test"));
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
similarity index 97%
rename from shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
rename to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
index 56f997e5e9b..9f11ca15ae3 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
@@ -31,7 +31,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
-public final class SM4EncryptAlgorithmTest {
+public final class SM4EncryptAlgorithmECBTest {
private EncryptAlgorithm<Object, String> encryptAlgorithm;
@@ -45,17 +45,17 @@ public final class SM4EncryptAlgorithmTest {
}
@Test
- public void assertEncryptWithECBAndPKCS5Padding() {
+ public void assertEncrypt() {
assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
}
@Test(expected = IllegalArgumentException.class)
public void assertEncryptWithoutKey() {
Properties props = new Properties();
- encryptAlgorithm.setProps(props);
- encryptAlgorithm.init();
props.setProperty("sm4-mode", "ECB");
props.setProperty("sm4-padding", "PKCS5Padding");
+ encryptAlgorithm.setProps(props);
+ encryptAlgorithm.init();
assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
}
@@ -72,10 +72,10 @@ public final class SM4EncryptAlgorithmTest {
@Test(expected = IllegalArgumentException.class)
public void assertDecryptWithoutKey() {
Properties props = new Properties();
- encryptAlgorithm.setProps(props);
- encryptAlgorithm.init();
props.setProperty("sm4-mode", "ECB");
props.setProperty("sm4-padding", "PKCS5Padding");
+ encryptAlgorithm.setProps(props);
+ encryptAlgorithm.init();
assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde", mock(EncryptContext.class)).toString(), is("test"));
}