How to Create Checksum Value in Java

A checksum is a small sized data created from a larger data set using an algorithm. If there is even a single change in the larger data set, the algorithm will create a different checksum value. This property can be used to verify the integrity of data. For example, you can host a large download on a website and then publish the checksum of the download on a separate location. Anyone downloading the file can regenerate the checksum and compare it with the published checksum. This reduces the risk of fake data, malware infected binaries etc.

Cryptographic hash functions such as MD5 and SHA256 can be used as the checksum function. These functions also ensure that two different data sets won't have the same checksum (no collision).

The following example Java program creates MD5 and SHA256 checksums from the given data. Note that SHA-1 algorithm is no longer recommended due to collision attacks possible (SHA-1 is broken). The following program also encodes the checksums into hexadecimal characters so that they are readable.

import java.security.MessageDigest;

import javax.xml.bind.DatatypeConverter;

// Creates MD5 and SHA-256 checksum in Java
public class ChecksumCalculator {

    public static void main(String[] args) {
        String data = "Hello World!";
        String md5Hash = getMD5Hash(data);
        String sha256hash = getSHA256Hash(data);
        
        System.out.println("data:"+data);
        System.out.println("md5:"+md5Hash);
        System.out.println("sha256:"+sha256hash);
    }

    // Java method to create SHA-25 checksum
    private static String getSHA256Hash(String data) {
        String result = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data.getBytes("UTF-8"));
            return bytesToHex(hash); // make it printable
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    // Java method to create MD5 checksum
    private static String getMD5Hash(String data) {
        String result = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] hash = digest.digest(data.getBytes("UTF-8"));
            return bytesToHex(hash); // make it printable
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    
    /**
     * Use javax.xml.bind.DatatypeConverter class in JDK to convert byte array
     * to a hexadecimal string. Note that this generates hexadecimal in lower case.
     * @param hash
     * @return 
     */
    private static String  bytesToHex(byte[] hash) {
        return DatatypeConverter.printHexBinary(hash).toLowerCase();
    }
}

The output of the program is given below. You should get the exact same checksum/hash.

data:Hello World!

md5:ed076287532e86365e841e92bfc50d8c

sha256:7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069

Verifying Checksum Using Commandline Tools

If you are on a mac machine, the following commands can be used to verify checksum.

md5 -s 'Hello World!'
echo -n 'Hello World!'|shasum -a 256