Generating MD5 hash in Java

After much messing, I’ve found the following way to generate an String MD5 hash of a String input in Java.

MessageDigest md5 = null;
String encodeMe = "password"; //string to be encoded

try {
  md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {


StringBuffer md5Buf = new StringBuffer();
for (Byte b : md5.digest()) {
  md5Buf.append(String.format("%x", b));

System.out.println("md5 = "+md5Buf.toString());