In this article, we will show you how to use a SHA-256 and MD5 algorithm to generate a checksum for a file.
- MessageDigest.getInstance(“algorithm”)
- Apache Commons Codec
1. MessageDigest
d:\server.log
hello world
1.1 Generate a file checksum with a SHA256 algorithm.
FileCheckSumSHA.java
package com.favtuts.crypto.hash; import java.io.FileInputStream; import java.io.IOException; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class FileChecksum { 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("/home/tvt/workspace/favtuts/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(); } }
Output
db42dbb4d57ff715c32d5b85fa8c340930590360f2db86280af75496c30b18a9
1.2 Generate a file checksum with a MD5 algorithm.
FileCheckSumMD5.java
package com.favtuts.crypto.hash; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class FileChecksum { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); String hex = checksum("/home/tvt/workspace/favtuts/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(); } }
Output
911e319109c6ff83cea342494e6599d4
2. Apache Commons Codec
pom.xml
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency>
2.1 File checksum.
FileCheckSum.java
package com.favtuts.crypto.hash; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.codec.digest.DigestUtils; public class FileChecksum { public static void main(String[] args) throws IOException { String filePath = "/home/tvt/workspace/favtuts/server.log"; String checksumSHA256 = DigestUtils.sha256Hex(new FileInputStream(filePath)); System.out.println("checksumSHA256 : " + checksumSHA256); String checksumMD5 = DigestUtils.md5Hex(new FileInputStream(filePath)); System.out.println("checksumMD5 : " + checksumMD5); } }
Output
checksumSHA256 : db42dbb4d57ff715c32d5b85fa8c340930590360f2db86280af75496c30b18a9
checksumMD5 : 911e319109c6ff83cea342494e6599d4
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-crypto/hash