File Transfer Check – MD5 Checksum Java Utility

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

public class MD5CheckSumUtility
{
/**
* This will check the MD5 Checking
* @param fileInput
* @return
* @throws Exception
*/
public String checkMD5(String fileInput)throws Exception
{
MessageDigest md = MessageDigest.getInstance(“MD5”);
FileInputStream fis = new FileInputStream(fileInput);
String result = “”;
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 method 1
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());
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i=0;i<mdbytes.length;i++) {
String hex=Integer.toHexString(0xff & mdbytes[i]);
if(hex.length()==1) hexString.append(‘0’);
hexString.append(hex);
}
//System.out.println(“Digest(in hex format):: ” + hexString.toString());
result = hexString.toString();
return result;
}

}

About ashokabhat

I am a C,C ,JAVA,Adobe Flex,.NET Programmer Currently working as a Software Developer
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s