Friday, April 23, 2010

Generate Hash of a file: Java

I google'ed/bing'ed a lot to find articles on generating a hash of a file and found none. So, i decided to write one myself.

In this article i explain about generating a hash for a file in Java.

We make use of "java.security.*" API classes to achieve the task.


In the following program i am generating the hash of the java source of this program (it need not be so, you can generate hash for any file).




The method

getInstance in 

MessageDigest class takes name of the hashing technique/Algorithm as a paramter

refer Java API at



http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html






1 comment:

  1. You could use the following org.apache.commons.codec.digest.DigestUtils.md5Hex(inputstream)

    http://commons.apache.org/codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html

    ReplyDelete