rain
2024-07-26 56cf48282b9fa39637fdcad9b445d2a2f81a61d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.dji.sample.territory.utils;
 
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
public class HashUtil {
    public static String SM3Hash(File file) {
        String filePath = String.valueOf(file);
        try {
            byte[] fileBytes = readFile(filePath);
            return sm3(fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static byte[] readFile(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        byte[] buffer = new byte[1024];
        int bytesRead;
        StringBuilder sb = new StringBuilder();
        while ((bytesRead = fis.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, bytesRead));
        }
        fis.close();
        return sb.toString().getBytes();
    }
    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")));
    }
}