How to generate a file checksum value in Java

Here is a simple example to demonstrate how to generate a file checksum value with “SHA-1” mechanism in Java.


import java.io.FileInputStream;
import java.security.MessageDigest;

public class TestCheckSum {
	
  public static void main(String args[]) throws Exception {
   
    String datafile = "c:\\INSTLOG.TXT";

    MessageDigest md = MessageDigest.getInstance("SHA1");
    FileInputStream fis = new FileInputStream(datafile);
    byte[] dataBytes = new byte[1024];
    
    int nread = 0; 
    
    while ((nread = fis.read(dataBytes)) != -1) {
      md.update(dataBytes, 0, nread);
    };

    byte[] mdbytes = md.digest();
   
    //convert the byte to hex format
    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());
    
  }
}

Result


Digest(in hex format):: bf35fa420d3e0f669e27b337062bf19f510480d4

The "INSTLOG.TXT" file has a "bf35fa420d3e0f669e27b337062bf19f510480d4" SHA-1 checksum value.

For checksum value in MD5 format , you need to change the MessageDigest :


MessageDigest md = MessageDigest.getInstance("MD5");

More detail about Message Digest Algorithms

Reference

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
KINGSLEY
Guest
KINGSLEY

i have been asked to produce a software which demands a person to enter either a text,file etc and produces in return hex from a sha 224 and 512 hash.i would love to do it in java but not only don’t i know how to call a file in a java program and i don’t how to go about writing the sha 224 and 512 programs.please help me

Shashant
Guest
Shashant

Nice post,
Worked well for txt files.
Can I find SHA-1 checksum for a encrypted pgp file or a zip file?

Thanks,
Shashant

Christian
Guest
Christian

Great work. Thank you!

Dawin
Guest
Dawin
I got java.lang.ArrayIndexOutOfBoundsException when I use this method in my code. I wonder why? It stopped when the current seq.No=977 in int byte[] seqNoinbytes=new byte[4]; seqNoinbytes=ByteBuffer.allocate(4).putInt(seqNo).array(); System.out.println("Sequence number= "+seqNo); for(int i=0;i<4;i++){ out_data[i]=seqNoinbytes[i]; System.out.println("Error after this, seqNoinbytes= "+(char)seqNoinbytes[i]); md.update(out_data, 0, seqNoinbytes[i]); //problem here }
Dawin
Guest
Dawin

Basically the seqNoinbytes[i] in md.update has value -47. I wonder how I can use this with a 4-bytes seq.No with value 977, and probably the other numbers.

anon
Guest
anon

Why are you adding 0x100?
If you simple use Integer.toString((byte & 0xFF), 16) you would get the same result.

rushman
Guest
rushman

Adding 0x100 to the number and converting it to String will preserve leading zeroes. That’s why substring(1) is needed to truncate the “hundreds”.
The result looks prettier, nothing else.

Example:

b + 0 = b –toString–> “b”
b + 100 = 10b –toString–> “10b” –substring–> “0b”

Interesting
Guest
Interesting

Interesting approach, how does it compare to using the CheckedInputStream?

Charlie Hayes
Guest
Charlie Hayes

You might consider using Adler32 instead of SHA1. It’s a whole lot faster. That is of-course unless you are using the checksum as some sort of identifier for the file, in which case the size of SHA1’s hash might be helpful.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/Adler32.html
http://www.anomalousanomaly.com/docs/CheckMark%20Results.pdf

software development company
Guest
software development company

Nice post,

exclent approch, I used the code and It worked fine

Thanks for bringing this up