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

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 90e99202d [type:optimize] optimized shaUtil (#4205)
90e99202d is described below

commit 90e99202d477c5a41163d528f4c7195507b34242
Author: 愿凌飞 <[email protected]>
AuthorDate: Wed Nov 23 20:03:47 2022 +0800

    [type:optimize] optimized shaUtil (#4205)
    
    Co-authored-by: Liming Deng <[email protected]>
---
 .../java/org/apache/shenyu/common/utils/ShaUtils.java     | 11 ++---------
 .../java/org/apache/shenyu/common/utils/ShaUtilsTest.java | 15 ++++++---------
 2 files changed, 8 insertions(+), 18 deletions(-)

diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ShaUtils.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ShaUtils.java
index 4105f7691..2c4536acf 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ShaUtils.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ShaUtils.java
@@ -21,6 +21,7 @@ import java.security.MessageDigest;
 
 import java.util.Optional;
 
+import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.common.exception.ShenyuException;
 
@@ -44,15 +45,7 @@ public class ShaUtils {
                 MessageDigest messageDigest = 
MessageDigest.getInstance("SHA-512");
                 messageDigest.update(item.getBytes());
                 byte[] byteBuffer = messageDigest.digest();
-                StringBuffer strHexString = new StringBuffer();
-                for (byte b:byteBuffer) {
-                    String hex = Integer.toHexString(0xff & b);
-                    if (hex.length() == 1) {
-                        strHexString.append('0');
-                    }
-                    strHexString.append(hex);
-                }
-                return strHexString.toString();
+                return Hex.encodeHexString(byteBuffer);
             } catch (Exception e) {
                 throw new ShenyuException(e);
             }
diff --git 
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ShaUtilsTest.java 
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ShaUtilsTest.java
index d70fc82c4..b4b203eab 100644
--- 
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ShaUtilsTest.java
+++ 
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ShaUtilsTest.java
@@ -18,6 +18,8 @@
 package org.apache.shenyu.common.utils;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -33,15 +35,10 @@ public final class ShaUtilsTest {
         assertThat(ShaUtils.shaEncryption("123456"), 
is("ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413"));
     }
 
-    @Test
-    public void testShaEncryptionForNull() {
-        assertThat(ShaUtils.shaEncryption(null), nullValue());
+    @ParameterizedTest
+    @NullAndEmptySource
+    public void testShaEncryptionForNullOrEmpty(final String src) {
+        assertThat(ShaUtils.shaEncryption(src), nullValue());
     }
-
-    @Test
-    public void testShaDecryptionForEmptyString() {
-        assertThat(ShaUtils.shaEncryption(""), nullValue());
-    }
-
 }
 

Reply via email to