Browse Source

修改加密过程

小车车 3 months ago
parent
commit
28f1ba9d08

+ 29 - 1
yudao-module-iscs/src/main/java/cn/iocoder/yudao/module/iscs/service/workflow/WorkflowModeServiceImpl.java

@@ -30,6 +30,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
+import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
 import java.util.Set;
@@ -180,14 +181,30 @@ public class WorkflowModeServiceImpl extends ServiceImpl<WorkflowModeMapper, Wor
         }
 
         // 开始写入.sha文件
-        String url1 = getPath() + currentTimestamp + ".sha";
+        /*String url1 = getPath() + currentTimestamp + ".sha";
         try (FileOutputStream fos = new FileOutputStream(url1)) {
             fos.write(s.getBytes()); // 写入字节数组
             System.out.println("数据已成功写入" + currentTimestamp + ".sha");
         } catch (IOException e) {
             e.printStackTrace();
+        }*/
+
+        // 写入 .dat 文件完成后,计算其哈希值
+        byte[] fileContentBytes = s.getBytes(); // 如果数据已在内存中,可直接复用
+        MessageDigest digest = MessageDigest.getInstance("SHA-256"); // 指定哈希算法
+        byte[] hashBytes = digest.digest(fileContentBytes); // 计算哈希值
+        String shaHex = bytesToHex(hashBytes); // 转为十六进制字符串
+
+        // 写入 .sha 文件
+        String url1 = getPath() + currentTimestamp + ".sha";
+        try (FileOutputStream fos = new FileOutputStream(url1)) {
+            fos.write(shaHex.getBytes()); // 写入哈希值而非原始数据
+            System.out.println("SHA256校验值已写入" + currentTimestamp + ".sha");
+        } catch (IOException e) {
+            e.printStackTrace();
         }
 
+
         // 开始压缩
         response.setContentType("application/zip");
         response.addHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(currentTimestamp + "zip"));
@@ -203,6 +220,17 @@ public class WorkflowModeServiceImpl extends ServiceImpl<WorkflowModeMapper, Wor
         }
     }
 
+    // 辅助方法:字节数组转十六进制字符串
+    private static String bytesToHex(byte[] bytes) {
+        StringBuilder hexString = new StringBuilder();
+        for (byte b : bytes) {
+            String hex = Integer.toHexString(0xff & b);
+            if (hex.length() == 1) hexString.append('0');
+            hexString.append(hex);
+        }
+        return hexString.toString();
+    }
+
     private void deleteFileByPath(String path) {
         File file = new File(path);
         file.delete();