Java獲取文件的hash值(SHA256)兩種方式
簡介
在工作開發(fā)當中需求要通過文件的hash值比對文件是否被篡改過,于是通過使用了(sha256)hash值進行比對,因為對于任意長度的消息,SHA256都會產(chǎn)生一個256bit長的哈希值,通常用一個長度為64的十六進制字符串來表示。
獲取網(wǎng)絡文件的sha256值(方式一)
首先通過InputStream獲取網(wǎng)絡URL文件,然后創(chuàng)建臨時文件,再通過FileInputStream以字節(jié)流的方式逐塊讀取文件內(nèi)容,然后通過DigestInputStream將讀取的數(shù)據(jù)傳遞給MessageDigest來計算SHA256哈希值。這樣可以避免將整個文件加載到內(nèi)存中,而是通過緩沖區(qū)逐塊處理文件內(nèi)容。
JAVA代碼如下:(文件地址自己改一下)
/** * 計算SHA256哈希值 * @param filePath 文件路徑 * @return 字節(jié)數(shù)組 * @throws IOException IO異常 * @throws NoSuchAlgorithmException NoSearch算法異常 */ public static byte[] calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-256"); //獲取網(wǎng)絡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é)數(shù)組轉(zhuǎn)換為String類型哈希值 * @param bytes 字節(jié)數(shù)組 * @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(); } }
響應結(jié)果:(這里的結(jié)果是我自己再測試的時候生成的)
SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98
獲取本地文件的sha256值(方式二)
通過FileInputStream以字節(jié)流的方式逐塊讀取文件內(nèi)容,然后通過DigestInputStream將讀取的數(shù)據(jù)傳遞給MessageDigest來計算SHA256哈希值。這樣可以避免將整個文件加載到內(nèi)存中,而是通過緩沖區(qū)逐塊處理文件內(nèi)容。
JAVA代碼如下:(文件地址自己改一下)
/** * 計算SHA256哈希值 * @param filePath 文件路徑 * @return 字節(jié)數(shù)組 * @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é)數(shù)組轉(zhuǎn)換為String類型哈希值 * @param bytes 字節(jié)數(shù)組 * @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(); } }
響應結(jié)果:(這里的結(jié)果是我自己再測試的時候生成的)
SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98
總結(jié)
到此這篇關于Java獲取文件的hash值(SHA256)兩種方式的文章就介紹到這了,更多相關Java獲取文件hash值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java 在Jetty9中使用HttpSessionListener和Filter
這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關資料,需要的朋友可以參考下2017-06-06java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup)
這篇文章主要介紹了java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03IntelliJ?IDEA2022中的Java文檔注釋設置、操作方法
這篇文章主要介紹了IntelliJ?IDEA2022中的Java文檔注釋設置、操作詳述,本文通過圖文并茂的方式給大家介紹IDEA2022?文檔注釋設置方法,需要的朋友可以參考下2022-08-08