I understand how it works but if I want to print out the MD5 as String how would I do that?
public static void getMD5(String fileName) throws Exception{
InputStream input = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
MessageDigest hash = MessageDigest.getInstance("MD5");
int read;
do {
read = input.read(buffer);
if (read > 0) {
hash.update(buffer, 0, read);
}
} while (read != -1);
input.close();
}
You can also use Apache Commons Codec library. This library includes methods
public static String md5Hex(InputStream data)
andpublic static String md5Hex(byte[] data)
in theDigestUtils
class. No need to invent this yourself ;)With the byte array, result from message digest:
Result (for example):
89e8a9f68ad3c4bba9b9d3581cf5201d
This is another version of @anything answer:
Try this
Shortest way:
Just remember to handle the exception.