Java SHA-256 Hashing Example

In this post, we will discuss how to calculate SHA-256 checksum for a file and input string using Java with suitable examples. The input file is a ~2GB file and let us compare the execution times at the end.

File Checksum with SHA-256 and FileInputStream 


In this example, we will use FileInputStream class with byte reads to generate the SHA hash. The Java code that does this is provided below:
/* This class is used to read the input file at byte level */
import java.io.FileInputStream; 
/* The class that generates the SHA output */
import java.security.MessageDigest; 
/* To create hex string from final byte array */
import org.apache.commons.codec.binary.Hex;
/* To measure timing */
import org.apache.commons.lang3.time.StopWatch; 
public class JavaSHACheckSumExample 
{
    public static void main(String[] args)throws Exception
    {
        StopWatch clock = new StopWatch();    
        MessageDigest myDigest = MessageDigest.getInstance("SHA-256");        
        FileInputStream inputFile = new FileInputStream("inputfile.zip");
        Hex finalHashString=new Hex();
        byte[] dataBytes = new byte[1024];
        int bytesFromFile;
        /* A stopwatch class to measure the time taken to read */
        clock.start();
        while ( (bytesFromFile=inputFile.read(dataBytes)) != -1 ) {
          myDigest.update(dataBytes,0,bytesFromFile);
        };
        clock.stop();
        byte[] mdDigestArray = myDigest.digest();
        System.out.println("Hex format : " + finalHashString.encodeHexString(mdDigestArray));
        System.out.println("Time to Read File: "+clock.getTime()+" milliseconds");
    }
}

It took 51405 milliseconds to generate SHA-256 hash in Java using FileInputStream class for a 2GB file.

SHA-256 File Checksum in Java Using BufferedInputStream


Here we use BufferedInputStream to generate the SHA-256 hash for the input file. For the same file it took 44474 milliseconds to generate the checksum.

/* This class is used to read the input file at byte level */
import java.io.FileInputStream; 
import java.io.BufferedInputStream;
/* The class that generates the SHA output */
import java.security.MessageDigest; 
/* To create hex string from final byte array */
import org.apache.commons.codec.binary.Hex;
/* To measure timing */
import org.apache.commons.lang3.time.StopWatch; 
public class BuffInputSHACheckSum 
{
    public static void main(String[] args)throws Exception
    {
        StopWatch clock = new StopWatch();    
        MessageDigest myDigest = MessageDigest.getInstance("SHA-256");  
        BufferedInputStream inputFile = new BufferedInputStream(new FileInputStream("inputfile.zip"));        
        Hex finalHashString=new Hex();
        byte[] dataBytes = new byte[1024];
        int bytesFromFile;
        /* A stopwatch class to measure the time taken to read */
        clock.start();
        while ( (bytesFromFile=inputFile.read(dataBytes,0,1024)) != -1 ) {
          myDigest.update(dataBytes,0,bytesFromFile);
        };
        clock.stop();
        byte[] mdDigestArray = myDigest.digest();
        System.out.println("Hex format : " + finalHashString.encodeHexString(mdDigestArray));
        System.out.println("Time to Read File: "+clock.getTime()+" milliseconds");
    }
}

Do you know any other methods to generate SHA-256 checksum at a faster rate? Post it here with your timings.

1 comment:

  1. You would require the following additional libraries to get this example working;

    a) Apache Commons Codec (To generate the file hash as hex string )
    b) Apache Commons Lang (Stopwatch)

    ReplyDelete