Java - Создать контрольную сумму файла с SHA и MD5
В этой статье мы покажем вам, как использовать алгоритм SHA-256 и MD5 для генерации контрольной суммы для файла.
-
MessageDigest.getInstance(“algorithm”)
-
Кодек Apache Commons
1. Дайджест сообщения
d:\server.log
hello world
1.1 Generate a file checksum with a SHA256 algorithm.
FileCheckSumSHA.java
package com.example.hashing; import java.io.FileInputStream; import java.io.IOException; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class FileCheckSumSHA { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("SHA-256"); //SHA, MD2, MD5, SHA-256, SHA-384... String hex = checksum("d:\\server.log", md); System.out.println(hex); } private static String checksum(String filepath, MessageDigest md) throws IOException { // file hashing with DigestInputStream try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; //empty loop to clear the data md = dis.getMessageDigest(); } // bytes to hex StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); } }
Выход
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
1.2 Generate a file checksum with a MD5 algorithm.
FileCheckSumMD5.java
package com.example.hashing; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class FileCheckSumMD5 { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); String hex = checksum("d:\\server.log", md); System.out.println(hex); } private static String checksum(String filepath, MessageDigest md) throws IOException { // DigestInputStream is better, but you also can hash file like this. try (InputStream fis = new FileInputStream(filepath)) { byte[] buffer = new byte[1024]; int nread; while ((nread = fis.read(buffer)) != -1) { md.update(buffer, 0, nread); } } // bytes to hex StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); } }
Выход
5eb63bbbe01eeed093cb22bb8f5acdc3
2. Кодек Apache Commons
pom.xml
commons-codec commons-codec 1.11
2.1 File checksum.
FileCheckSum.java
package com.example.hashing; import org.apache.commons.codec.digest.DigestUtils; import java.io.FileInputStream; import java.io.IOException; public class App { public static void main(String[] args) throws IOException { String checksumSHA256 = DigestUtils.sha256Hex(new FileInputStream("d:\\server.log")); System.out.println("checksumSHA256 : " + checksumSHA256); String checksumMD5 = DigestUtils.md5Hex(new FileInputStream("d:\\server.log")); System.out.println("checksumMD5 : " + checksumMD5); } }
Выход
checksumSHA256 : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 checksumMD5 : 5eb63bbbe01eeed093cb22bb8f5acdc3