package org.sxkj.common.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();
|
}
|
|
}
|