How to Calculate the SHA Hash Value of a File in Java


Calculating SHA hash value of a file is really helpful for its data integrity.

You will notice that the software setups or files shared on the website that you download have their hash values displayed on the download pages. This hash can be used to compare the hash of the downloaded or transferred file to know if the downloaded file can be trusted, if they match, you can ensure that the file has not been tampered with during transmission or storage.

If the hashes of two files match it can be assured that the file is unchanged and untampered.

In this example, we take a look at how to calculate the SHA hash value for two files in Java.

import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * Java example to Calculate SHA hash
 * value of two files to see if they
 * have the same content or not
 *
 * Author: Code2care.org
 * Version: v1
 * Date: 07/10/2023
 *
 */
public class FileSHAComparisonExample {

    private static final String SHA_ALGORITHM = "SHA-256";
    private static final int BUFFER_SIZE = 8192;

    public static void main(String[] args) {

        String file1Path = "/Users/dev/IdeaProjects/basics/src/data_2023.csv";
        String file2Path = "/Users/dev/IdeaProjects/basics/src/data_2024.csv";

        try {

            byte[] file1Hash = calculateSHA256(file1Path);
            byte[] file2Hash = calculateSHA256(file2Path);

            boolean areFilesEqual = Arrays.equals(file1Hash, file2Hash);

            if (areFilesEqual) {
                System.out.println("The files have the same content.");
            } else {
                System.out.println("The files have different content.");
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static byte[] calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException {

        MessageDigest messageDigest = MessageDigest.getInstance(SHA_ALGORITHM);

        try (FileInputStream inputStream = new FileInputStream(filePath)) {
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                messageDigest.update(buffer, 0, bytesRead);
            }
        }

        return messageDigest.digest();
    }
}
data_2023.csv-file
data_2024.csv-file

Output:

The files have the same content.

Now let's change the contents of one of the files and run the code again.

Output:

The files have different content.

SHA AlgorithmDescriptionHash Size
SHA-1Secure Hash Algorithm 1160 bits
SHA-224SHA-2 with 224-bit hash224 bits
SHA-256SHA-2 with 256-bit hash256 bits
SHA-384SHA-2 with 384-bit hash384 bits
SHA-512SHA-2 with 512-bit hash512 bits
SHA-512/224SHA-2 with 224-bit hash (512)224 bits
SHA-512/256SHA-2 with 256-bit hash (512)256 bits
SHA-3-224SHA-3 with 224-bit hash224 bits
SHA-3-256SHA-3 with 256-bit hash256 bits
SHA-3-384SHA-3 with 384-bit hash384 bits
SHA-3-512SHA-3 with 512-bit hash512 bits

Some examples of SHA hashes for the data_2023.csv file.

SHA-1 Hash: ffee510fa54db38070bf2fa9f07761e7666c1ebc
SHA-384 Hash: 07b73d610d6632556e195dc1687c6a3278ee14f588ca199598ed998065af1fc09f36acf20d05eb213b603c850497aaf2
SHA-224 Hash: ac322d5fb2c85630000952b0b428d95f4ea1d5469c461cca8e9205e1
SHA-256 Hash: 6171f17906ba66cb81255085fb2ef6203435549d41aeaf9c11640d410888ea23
SHA-512 Hash: 0fcb2d37e0877162cf870bf70143005cda762a5ee0e0f969a19406b029cf8926e53037a735ba76b9d8c3402f9002024995e0f5c3b4d301cb3bd8f22496552555

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap