How to generate file checksum value in Java
In this tutorial, we will learn how to generate checksum values for the files in Java.
Before diving deep into the topic, let us first get acquainted with the term checksum value.
A checksum value is an encrypted sequence of characters that is attained after the application of certain algorithms and operations on content provided by the user.
The value is crucial with respect to the safety and privacy of the file. It helps to detect any sort of file tampering.
In Java, you can generate a checksum value for a file using the MessageDigest class. The most commonly used algorithms for generating a checksum value are MD5 and SHA-1.
MD5 Method
MD5 (Message Digest 5) is a cryptographic hash function that takes in input in the form of a file, message, or any data of arbitrary length and produces a fixed-size output of 128 bits, called the hash or message digest.
Algorithm:
Here is the algorithm for generating the checksum value of a file using the MD5 algorithm:
Step 1: Import the necessary classes:
Step 2: Define a class called FileChecksum1 with a main() method.
Step 3: Within the main() method, create a FileInputStream object to read the input file.
It is to be noted that the user has to replace "file.txt" with the path to their own file.
Step 4: Create a MessageDigest instance for the algorithm you want to use (in this case, MD5):
Step 5: Read the file in chunks using a while loop.
Step 6: Get the checksum value by calling the digest() method on the MessageDigest instance.
Step 7: Convert the checksum bytes to a hexadecimal string:
Step 8: Print out the checksum value.
Step 9: Close the input stream.
Implementation:
It is an implementation to generate the MD5 checksum for a file:
File name – FileChecksum1.java
import java.io.FileInputStream;
import java.security.MessageDigest;
public class FileChecksum1 {
public static void main(String[] args) throws Exception {
// Opening the file
FileInputStream inputStream = new FileInputStream("file.txt");
// Get a MessageDigest instance for MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// Reads the file in portions of 1024 bytes
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
md.update(buffer, 0, bytesRead);
}
// Get the checksum value
byte[] checksum = md.digest();
// Conversion of the checksum to a hexadecimal string
StringBuilder sb = new StringBuilder();
for (byte b : checksum) {
sb.append(String.format("%02x", b));
}
String checksumValue = sb.toString();
System.out.println("MD5 checksum value: " + checksumValue);
// Closure of the input stream
inputStream.close();
}
}
Output:

SHA-1 Method
SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that takes in an input (which can be a file, message, or any data) of arbitrary length and produces a fixed-size output of 160 bits, called the hash or message digest.
In general, SHA-1 is considered to be more secure than MD5. While both algorithms have been shown to have weaknesses, SHA-1 is less vulnerable to collision attacks than MD5.
Algorithm:
Here is the algorithm for generating the checksum value of a file using the SHA-1 algorithm :
Steps:
- Create a FileInputStream object to read the input file.
- Create a MessageDigest instance for the SHA-1 algorithm using MessageDigest.getInstance("SHA-1").
- Create a buffer to read the file in chunks. In this example, we will use a buffer size of 1024 bytes.
- Read the file in chunks using a while loop. In each iteration of the loop, read up to 1024 bytes of data from the file into the buffer using inputStream.read(buffer). If the read method returns -1, it means that we have reached the end of the file, and we can exit the loop.
- Update the MessageDigest instance with the data in the buffer using messageDigest.update(buffer, 0, bytesRead), where bytesRead is the number of bytes that were read from the file in the current iteration.
- After reading the entire file, call the digest() method on the MessageDigest instance to generate the SHA-1 checksum value. This will return a byte array containing the checksum value.
- Convert the checksum bytes to a hexadecimal string using String.format("%02x", byteValue).
- Close the input stream using inputStream.close().
- Return the hexadecimal checksum value.
Implementation:
It is an implementation to generate the SH1 checksum for a file:
File name – FileChecksum2.java
import java.io.FileInputStream;
import java.security.MessageDigest;
public class FileChecksum2 {
public static void main(String[] args) throws Exception {
// Define the path to the input file
String filePath = "file.txt";
// Creation of a FileInputStream object to read the input file
FileInputStream inputStream = new FileInputStream(filePath);
// Creation of a MessageDigest instance for the SHA-1 algorithm
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
// Read the file in chunks using a while loop
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
sha1.update(buffer, 0, bytesRead);
}
// Getting the checksum value by calling the digest() method on the MessageDigest instance
byte[] checksum = sha1.digest();
// Conversion of the checksum bytes to a hexadecimal string
StringBuilder sb = new StringBuilder();
for (byte b: checksum) {
sb.append(String.format("%02x", b));
}
String checksumValue = sb.toString();
// Display the checksum value
System.out.println("SHA-1 checksum value: " + checksumValue);
// Closure of the input stream
inputStream.close();
}
}
Output:

SHA-256
SHA-256 is a cryptographic hash function that takes in an input (which can be a file, message, or any data) of arbitrary length and produces a fixed-size output of 256 bits, called the hash or message digest.
Algorithm:
Here is the algorithm of the above code that generates the SHA-256 checksum value of a file and writes it to a file:
Steps:
- Define the path to the input file and output file.
- Create a FileInputStream object to read the input file.
- Create a MessageDigest instance for the SHA-256 algorithm using the MessageDigest.getInstance("SHA-256") method.
- Create a DigestInputStream object for the SHA-256 algorithm using the input stream and the MessageDigest instance.
- Read the file in chunks using a while loop and update the MessageDigest instance with the read bytes using the update() method.
- Get the checksum value by calling the digest() method on the MessageDigest instance.
- Write the checksum value to the output file using a FileOutputStream.
- Close the input and output streams.
Implementation:
It is an implementation to generate the SH1 checksum for a file.
File name – FileChecksum3.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
public class FileChecksum3 {
public static void main(String[] args) throws Exception {
// Define the path to the input file and output file
String inputFilePath = "file.txt";
String outputFilePath = "checksum.sha256";
// Creation of a FileInputStream object to read the input file
FileInputStream inputStream = new FileInputStream(inputFilePath);
// Creation of a DigestInputStream object for the SHA-256 algorithm
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
DigestInputStream digestInputStream = new DigestInputStream(inputStream, sha256);
// Reading the file in chunks using a while loop
byte[] buffer = new byte[1024];
while (digestInputStream.read(buffer) != -1);
// Get the checksum value by calling the digest() method on the MessageDigest instance
byte[] checksum = sha256.digest();
// Writing the checksum value to the output file
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
outputStream.write(checksum);
outputStream.close();
// Closure of the input stream
digestInputStream.close();
}
}
Output:

Conclusion
In this tutorial, we learned to use SH1 and MD5 methods to generate the checksum value of files. However, both methods have got certain flaws.
Therefore, SHA-256 or SHA-3 are recommended as they provide better security and are less vulnerable to attacks.
It is to be noted that the actual checksum value may be different depending on the contents of the input file.