01
Jul '11

Pass it a filename and it’ll pass you back the Hash. Simple as that!

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public String getMD5Hash(String fileName) {
	String hash = "";

	try {
		File file = new File(fileName);
		FileReader fileRead = new FileReader(file);
		MessageDigest md = MessageDigest.getInstance("MD5");

		char[] chars = new char[1024];
		int charsRead = 0;

		do {
			charsRead = fileRead.read(chars, 0, chars.length);

			if(charsRead >= 0) {
				md.update(new String(chars, 0, charsRead).getBytes());
			}
		}
		while(charsRead >= 0);

		hash = new BigInteger(1, md.digest()).toString(16);
	}
	catch (NoSuchAlgorithmException e){}
	catch (IOException e){}

	return hash;
}