How to Generate MD5 Checksum for Files in Java?


Checksum is an encrypted sequence of characters that are generated with the help of various hashing algorithms like MD5 and SHA-1. The checksum is applicable for the downloadable files. Whenever we generate a checksum for files, it stays the same as long as the file does not get altered or corrupted. In this article, we are going to explore the MD5 checksum which is a hash value used to verify the integrity of files. It is a way of ensuring that the file one has downloaded or transferred is exactly the same as the original one. To generate MD5 checksum for files in Java we can use the built-in java.security.MessageDigest class.

Java Program to Generate MD5 Checksum for Files

Before starting the discussion of the example program, it is necessary to understand a few concepts that we will use to generate the MD5 checksum.

MessageDigest class

Let's begin with hashing methods that are used for converting an input into another encrypted value and this returned value is known as a message digest or hash value. In Java, we have a class named MessageDigest that handles the message digest algorithm like MD5.

To use this class, we need to import it using the following command:

import java.security.MessageDigest;

Methods of MessageDigest

  • getInstance(): It accepts a parameter in the form String specifying the name of the message digest algorithm. It returns an instance of MessageDigest class along with the given algorithm.

  • update(): It accepts a byte array representing the information from a file and passes it to the instance of MessageDigest class.

  • digest(): This method is used with the instance of MessageDigest class and generates the message digest in the form of a byte array.

Example

The following example demonstrates the practical implementation of how to generate MD5 Checksum for files.

Approach

  • First, import the required packages so that we can work with FileInputStream and MessageDigest classes.

  • Inside the main method, define a try block to handle the required operations. Within this block, create an instance of MessageDigest class by passing 'MD5' algorithm as a parameter.

  • Then, provide the path of file for which we need to generate the checksum.

  • Use the 'update()' method to add the information of file to the MessageDigest instance.

  • Now, use the 'digest()' method to generate the checksum and store it in a byte array.

  • Create an instance of the StringBuilder class to store the converted hash value from the byte array.

  • Use a for-each loop that will iterate through the whole byte array and convert it into a hexadecimal string.

  • Moving further convert the StringBuilder into string and print it.

import java.io.*;
import java.security.*;
public class MD5Example {
   public static void main(String[] args) {
      try {
         // Creating an instance of MessageDigest with MD5 
         MessageDigest mdgst = MessageDigest.getInstance("MD5");
         // Creating an instance of FileInputStream 
         FileInputStream fistrm = new FileInputStream("D:/Java Programs/myTextfile.txt");
         // Updating the message digest with the file bytes
         mdgst.update(fistrm.readAllBytes());
         // generating checksum in the form of byte array
         byte[] hash = mdgst.digest();
         // converting byte array into hexadecimal string
         StringBuilder sbstr = new StringBuilder(); 
         // loop to iterate through hash value
         for (byte value : hash) {
            // appending value to the StringBuilder
            sbstr.append(String.format("%02x", value));
         }
         // converting StringBuilder into string
         String hex = sbstr.toString();
         // printing hexadecimal string
         System.out.println("The generated MD5 checksum of given file: " + hex);
	     // to handle the exception if any
      } catch (NoSuchAlgorithmException | IOException exp) {
         System.out.println(exp);
      }
   }
}

Output

The generated MD5 checksum of given file: 239fa62210aa38f502f04f4008211e64

Conclusion

In this article, we have learned what is checksum and its use in securing files. Also, we discovered the MessageDigest class of Java that provides MD5 message digest algorithm to validate specified files by generating checksum. In the end, we discussed a Java program that showed us how to generate the MD5 Checksum for files.

Updated on: 20-Jul-2023

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements