You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/01/13 10:08:40 UTC

[GitHub] [shardingsphere] strongduanmu opened a new pull request #14743: Fix SM4EncryptAlgorithm check exception

strongduanmu opened a new pull request #14743:
URL: https://github.com/apache/shardingsphere/pull/14743


   Fixes #14739.
   
   Changes proposed in this pull request:
   - fix SM4EncryptAlgorithm check exception
   - optimize SM4EncryptAlgorithm and SM3EncryptAlgorithm init logic
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] TeslaCN commented on a change in pull request #14743: Fix SM4EncryptAlgorithm check exception

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on a change in pull request #14743:
URL: https://github.com/apache/shardingsphere/pull/14743#discussion_r783819813



##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
##########
@@ -82,60 +133,32 @@ public Object decrypt(final String cipherValue) {
         if (null == cipherValue) {
             return null;
         }
-        return StringUtils.newStringUtf8(decrypt(Hex.decodeHex(cipherValue.toCharArray())));
+        return StringUtils.newStringUtf8(decrypt(ByteUtils.fromHexString(cipherValue)));
     }
     
     private byte[] decrypt(final byte[] cipherValue) {
-        return sm4(cipherValue, Cipher.DECRYPT_MODE);
-    }
-    
-    @Override
-    public void init() {
-    }
-    
-    @Override
-    public String getType() {
-        return SM4;
+        return handle(cipherValue, Cipher.DECRYPT_MODE);
     }
 
     @SneakyThrows
-    private byte[] sm4(final byte[] input, final int mode) {
-        String modeAndPadding = String.format("SM4/%s/%s", checkAndGetMode(), checkAndGetPadding());
-        Cipher cipher = Cipher.getInstance(modeAndPadding, org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
-        SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decodeHex(checkAndGetKey().toCharArray()), SM4);
-        String iv = checkAndGetIv(modeAndPadding);
-        if (null != iv) {
-            cipher.init(mode, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
+    private byte[] handle(final byte[] input, final int mode) {
+        Cipher cipher = Cipher.getInstance(String.format("SM4/%s/%s", sm4Mode, sm4Padding), BouncyCastleProvider.PROVIDER_NAME);

Review comment:
       I suggest using `+` to concat String in high frequency code.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] TeslaCN commented on a change in pull request #14743: Fix SM4EncryptAlgorithm check exception

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on a change in pull request #14743:
URL: https://github.com/apache/shardingsphere/pull/14743#discussion_r783819813



##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
##########
@@ -82,60 +133,32 @@ public Object decrypt(final String cipherValue) {
         if (null == cipherValue) {
             return null;
         }
-        return StringUtils.newStringUtf8(decrypt(Hex.decodeHex(cipherValue.toCharArray())));
+        return StringUtils.newStringUtf8(decrypt(ByteUtils.fromHexString(cipherValue)));
     }
     
     private byte[] decrypt(final byte[] cipherValue) {
-        return sm4(cipherValue, Cipher.DECRYPT_MODE);
-    }
-    
-    @Override
-    public void init() {
-    }
-    
-    @Override
-    public String getType() {
-        return SM4;
+        return handle(cipherValue, Cipher.DECRYPT_MODE);
     }
 
     @SneakyThrows
-    private byte[] sm4(final byte[] input, final int mode) {
-        String modeAndPadding = String.format("SM4/%s/%s", checkAndGetMode(), checkAndGetPadding());
-        Cipher cipher = Cipher.getInstance(modeAndPadding, org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
-        SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decodeHex(checkAndGetKey().toCharArray()), SM4);
-        String iv = checkAndGetIv(modeAndPadding);
-        if (null != iv) {
-            cipher.init(mode, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
+    private byte[] handle(final byte[] input, final int mode) {
+        Cipher cipher = Cipher.getInstance(String.format("SM4/%s/%s", sm4Mode, sm4Padding), BouncyCastleProvider.PROVIDER_NAME);

Review comment:
       For performance, I suggest using `+` to concat String in high frequency code.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #14743: Fix SM4EncryptAlgorithm check exception

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on a change in pull request #14743:
URL: https://github.com/apache/shardingsphere/pull/14743#discussion_r783821149



##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
##########
@@ -82,60 +133,32 @@ public Object decrypt(final String cipherValue) {
         if (null == cipherValue) {
             return null;
         }
-        return StringUtils.newStringUtf8(decrypt(Hex.decodeHex(cipherValue.toCharArray())));
+        return StringUtils.newStringUtf8(decrypt(ByteUtils.fromHexString(cipherValue)));
     }
     
     private byte[] decrypt(final byte[] cipherValue) {
-        return sm4(cipherValue, Cipher.DECRYPT_MODE);
-    }
-    
-    @Override
-    public void init() {
-    }
-    
-    @Override
-    public String getType() {
-        return SM4;
+        return handle(cipherValue, Cipher.DECRYPT_MODE);
     }
 
     @SneakyThrows
-    private byte[] sm4(final byte[] input, final int mode) {
-        String modeAndPadding = String.format("SM4/%s/%s", checkAndGetMode(), checkAndGetPadding());
-        Cipher cipher = Cipher.getInstance(modeAndPadding, org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
-        SecretKeySpec secretKeySpec = new SecretKeySpec(Hex.decodeHex(checkAndGetKey().toCharArray()), SM4);
-        String iv = checkAndGetIv(modeAndPadding);
-        if (null != iv) {
-            cipher.init(mode, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
+    private byte[] handle(final byte[] input, final int mode) {
+        Cipher cipher = Cipher.getInstance(String.format("SM4/%s/%s", sm4Mode, sm4Padding), BouncyCastleProvider.PROVIDER_NAME);

Review comment:
       > 
   
   @TeslaCN Thank you, I will optimize it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] tristaZero merged pull request #14743: Fix SM4EncryptAlgorithm check exception

Posted by GitBox <gi...@apache.org>.
tristaZero merged pull request #14743:
URL: https://github.com/apache/shardingsphere/pull/14743


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org