Calculating MD5 hash for a File


Below Code can be used for calculating md5 hash of a file. It uses 2 libraries from apache commons project. One is common-codec and other one is commons-io.

package com.wordpress.narendrakadali.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

/**
* @author narendra
*/
public class Main
{

/**
* @param args
*/
public static void main(String args[]) throws Exception
{
String md5 = null;
final String FILE_NAME = "N:/ORGANIZED DATA Large Sized/ORGANIZED DATA/Project_Soft.iso";
FileInputStream fis;
try
{
fis = new FileInputStream(new File(FILE_NAME));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
try
{
md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(IOUtils.toByteArray(fis));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}

System.out.println("MD5 value of file:: " + FILE_NAME + " is:: " + md5);
}
}

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: