2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

generate a file checksum value using java

A checksum or hash sum is a small-size datum from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage. It is usually applied to an installation file after it is received from the download server. By themselves checksums are often used to verify data integrity, but should not be relied upon to also verify data authenticity.

To generate a file checksum value with "SHA-1" mechanism in Java.

package com.kodetalk;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Abhinab 13 February 2016
 *
 */
public class Checksum {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        String datafile = "c:" + File.separator + "test" + File.separator + "SCRIPT.txt";
        MessageDigest md = MessageDigest.getInstance("SHA1");
        @SuppressWarnings("resource")
        FileInputStream fileInputStream = new FileInputStream(datafile);
        byte[] dataBytes = new byte[1024];
        int nread = 0;
        while ((nread = fileInputStream.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }
        byte[] mdbytes = md.digest();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        System.out.println("Digest(in hex format) :: " + sb.toString());
    }
}

The output will be as below:

Digest(in hex format) :: d3559ebaef6d56ed508a381b501bfb7891598d9f

Checksums are used to ensure the integrity of a file after it has been transmitted from one storage device to another. This can be across the Internet or simply between two computers on the same network. Either way, if we want to ensure that the transmitted file is exactly the same as the source file, we can use a checksum.
The checksum is calculated using a hash function [may be "SHA-1"] and is normally posted along with the download. To verify the integrity of the file, a user calculates the checksum using a checksum calculator program and then compares the two to make sure they match. Checksums are used not only to ensure a corrupt-free transmission, but also to ensure that the file has not been tampered with. When a good checksum algorithm is used, even a tiny change to the file will result in a completely different checksum value.