erdengk commented on a change in pull request #2928:
URL: https://github.com/apache/incubator-shenyu/pull/2928#discussion_r812955733



##########
File path: 
shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/ShaUtils.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.shenyu.admin.utils;
+
+import java.security.MessageDigest;
+
+import java.util.Optional;
+import org.apache.shenyu.common.exception.ShenyuException;
+
+/**
+ * The type SHA utils.
+ */
+public class ShaUtils {
+
+    /**
+     * sh512 Encryption string.
+     *
+     * @param src the src
+     * @return the string
+     */
+    public static String shaEncryption(final String src) {
+        return Optional.ofNullable(src).map(item -> {
+            if ("".equals(src)) {
+                return null;
+            }
+            try {
+                MessageDigest messageDigest = 
MessageDigest.getInstance("SHA-512");
+                messageDigest.update(item.getBytes());
+                byte[] byteBuffer = messageDigest.digest();
+                StringBuffer strHexString = new StringBuffer();
+                for (byte b:byteBuffer) {

Review comment:
       Here is my current improved code that addresses the for mentioned above. 
The following code can complete the encryption function.
   But it introduces a for loop that converts byte[] to List<Byte> .
   I checked the related issues online. But still can't convert byte[] to 
List<Byte> via lambda.
   can you help me?
   
   ```java
   public static String shaEncryption(final String src) {
           return Optional.ofNullable(src).map(item -> {
               if (StringUtils.isEmpty(src)) {
                   return null;
               }
               try {
                     MessageDigest messageDigest = 
MessageDigest.getInstance("SHA-512");
                     messageDigest.update(s.getBytes());
                     byte[] byteBuffer = messageDigest.digest();
   //  introduce a for loop  
                     List<Byte> list = new ArrayList<>();
                     for (byte b:byteBuffer){
                         list.add(b);
                     }
   
                     String res = list.stream().map(b->Integer.toHexString(0xff 
& b)).collect(Collectors.joining(""));
                     return res;
               } catch (Exception e) {
                   throw new ShenyuException(e);
               }
           }).orElse(null);
       }
   ```




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to