From 94174d2cc22afed6f41c270d970903484bfc5708 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 05 Aug 2024 16:06:18 +0800
Subject: [PATCH] SM2测试类

---
 src/main/java/com/dji/sample/territory/utils/HashUtil.java |   26 +++++++++++++-------------
 1 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/dji/sample/territory/utils/HashUtil.java b/src/main/java/com/dji/sample/territory/utils/HashUtil.java
index d0d1f0f..954a929 100644
--- a/src/main/java/com/dji/sample/territory/utils/HashUtil.java
+++ b/src/main/java/com/dji/sample/territory/utils/HashUtil.java
@@ -10,18 +10,15 @@
 public class HashUtil {
     public static String SM3Hash(File file) {
         String filePath = String.valueOf(file);
-        String hashHex = "";
         try {
             byte[] fileBytes = readFile(filePath);
-            byte[] hash = calculateHash(fileBytes);
-             hashHex = Hex.toHexString(hash);
+            return sm3(fileBytes);
         } catch (IOException e) {
             e.printStackTrace();
         }
-        return hashHex;
+        return "";
     }
-
-    private static byte[] readFile(String filePath) throws IOException {
+    public static byte[] readFile(String filePath) throws IOException {
         FileInputStream fis = new FileInputStream(filePath);
         byte[] buffer = new byte[1024];
         int bytesRead;
@@ -32,12 +29,15 @@
         fis.close();
         return sb.toString().getBytes();
     }
-
-    private static byte[] calculateHash(byte[] input) {
-        SM3Digest digest = new SM3Digest();
-        digest.update(input, 0, input.length);
-        byte[] hash = new byte[digest.getDigestSize()];
-        digest.doFinal(hash, 0);
-        return hash;
+    public static String sm3(byte[] data) {
+        byte[] md = new byte[32];
+        SM3Digest sm3 = new SM3Digest();
+        sm3.update(data, 0, data.length);
+        sm3.doFinal(md, 0);
+        String s = new String(Hex.encode(md));
+        return s.toUpperCase();
+    }
+    public static void main(String[] args) {
+        System.out.println(SM3Hash(new File("src/main/resources/FJ_11.jpeg")));
     }
 }

--
Gitblit v1.9.3