I needed to create MD5 hashes to populate a password database for apache. This seemed like a very simple thing. So, when I wanted an MD5 hash in hex for my JSP app I expected to find a utility ready and waiting inside java. No such luck. No problem, I thought — I’ll just “google” it.
I was surprised to find that there are lots of half-baked solutions out there posted on various discussion forums, but none of them were solid, simple, and self-explanatory; or at least they didn’t look like code I would be comfortable with. So I decided to write it up and make it easy to find just in case someone else has the same experience.
My solution breaks out into three tiny functions that might be re-used lots of places.
import java.util.*;
import java.security.*;
String HexForByte(byte b) {
String Hex = Integer.toHexString((int) b & 0xff);
boolean hasTwoDigits = (2 == Hex.length());
if(hasTwoDigits) return Hex;
else return "0" + Hex;
}
String HexForBytes(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for(byte b : bytes) sb.append(HexForByte(b));
return sb.toString();
}
String HexMD5ForString(String text) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(text.getBytes());
return HexForBytes(digest);
}
HexForByte(byte b) gives you a two digit hex string for any byte. This is important because Integer.toHexString() will give you only one digit if the input is less than 16. That can be a real problem if you are building hash strings. Another tricky bit in this function strips the sign from the byte before converting it to an integer. In java, every kind of number is signed so we have to watch out for that when making conversions.
HexForBytes(byte[] bytes) gives you a hex string for any array of bytes. Each byte will be correctly represented by precisely two hex digits. No more, no less.
Wrapping it all up, HexMD5ForString(String text) gives you an MD5 digest in hex of any string. According to the apache documentation this is what I will want to put into the database so that mod_auth_digest can authenticate users of my web app. To see what started all of this look here: http://httpd.apache.org/docs/2.4/misc/password_encryptions.html
With the code above in place I can now do something like:
HexMD5ForString( user + ":" + realm + ":" + password );
From the look of it, the Java code on the apache page looks like it will work; and it may be faster; but doing it my way the code is less obscure and yields a few extra utility functions that can be useful other places.