Java獲取文件的hash值(SHA256)兩種方式
簡介
在工作開發(fā)當中需求要通過文件的hash值比對文件是否被篡改過,于是通過使用了(sha256)hash值進行比對,因為對于任意長度的消息,SHA256都會產生一個256bit長的哈希值,通常用一個長度為64的十六進制字符串來表示。
獲取網絡文件的sha256值(方式一)
首先通過InputStream獲取網絡URL文件,然后創(chuàng)建臨時文件,再通過FileInputStream以字節(jié)流的方式逐塊讀取文件內容,然后通過DigestInputStream將讀取的數據傳遞給MessageDigest來計算SHA256哈希值。這樣可以避免將整個文件加載到內存中,而是通過緩沖區(qū)逐塊處理文件內容。
JAVA代碼如下:(文件地址自己改一下)
/**
* 計算SHA256哈希值
* @param filePath 文件路徑
* @return 字節(jié)數組
* @throws IOException IO異常
* @throws NoSuchAlgorithmException NoSearch算法異常
*/
public static byte[] calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//獲取網絡URL文件
InputStream fis2 = new URL(filePath).openStream();
//創(chuàng)建臨時文件
File file = File.createTempFile(IdWorker.getIdStr(),"");
FileUtil.writeFromStream(fis2,file);
try (
FileInputStream fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
DigestInputStream dis = new DigestInputStream(fis, digest)) {
ByteBuffer buffer = ByteBuffer.allocate(8192); // 8 KB buffer
while (channel.read(buffer) != -1) {
buffer.flip();
digest.update(buffer);
buffer.clear();
}
return digest.digest();
}
}
/**
* 將字節(jié)數組轉換為String類型哈希值
* @param bytes 字節(jié)數組
* @return 哈希值
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
public static void main(String[] args) {
String filePath = "https://xxxxx/20230410/bfd71f584d9645b0a5e3d7a465119f0c.pdf";
try {
byte[] sha256 = calculateSHA256(filePath);
String sha256Hex = bytesToHex(sha256);
System.out.println("SHA256: " + sha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}響應結果:(這里的結果是我自己再測試的時候生成的)
SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98
獲取本地文件的sha256值(方式二)
通過FileInputStream以字節(jié)流的方式逐塊讀取文件內容,然后通過DigestInputStream將讀取的數據傳遞給MessageDigest來計算SHA256哈希值。這樣可以避免將整個文件加載到內存中,而是通過緩沖區(qū)逐塊處理文件內容。
JAVA代碼如下:(文件地址自己改一下)
/**
* 計算SHA256哈希值
* @param filePath 文件路徑
* @return 字節(jié)數組
* @throws IOException IO異常
* @throws NoSuchAlgorithmException NoSearch算法異常
*/
public static byte[] calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (
FileInputStream fis = new FileInputStream(filePath);
FileChannel channel = fis.getChannel();
DigestInputStream dis = new DigestInputStream(fis, digest)) {
ByteBuffer buffer = ByteBuffer.allocate(8192); // 8 KB buffer
while (channel.read(buffer) != -1) {
buffer.flip();
digest.update(buffer);
buffer.clear();
}
return digest.digest();
}
}
/**
* 將字節(jié)數組轉換為String類型哈希值
* @param bytes 字節(jié)數組
* @return 哈希值
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
public static void main(String[] args) {
String filePath = "D:\\bfd71f584d9645b0a5e3d7a465119f0c.pdf";
try {
byte[] sha256 = calculateSHA256(filePath);
String sha256Hex = bytesToHex(sha256);
System.out.println("SHA256: " + sha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}響應結果:(這里的結果是我自己再測試的時候生成的)
SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98
總結
到此這篇關于Java獲取文件的hash值(SHA256)兩種方式的文章就介紹到這了,更多相關Java獲取文件hash值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談springboot @Repository與@Mapper的區(qū)別
本文主要介紹了淺談springboot @Repository與@Mapper的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Quarkus中RESTEasy?Reactive集成合并master分支
這篇文章主要為大家介紹了Quarkus中RESTEasy?Reactive集成合并master分支的詳細作用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02
Springboot升級至2.4.0中出現(xiàn)的跨域問題分析及修改方案
這篇文章主要介紹了Springboot升級至2.4.0中出現(xiàn)的跨域問題分析及修改方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

