The request was to have a Java function generating the same MD5 string generated by PHP when using the md5() function. Here the code snippet:

MessageDigest md = MessageDigest.getInstance("MD5");
md.update(value.getBytes("UTF-8"));
BigInteger hash = new BigInteger(1, md.digest());
String result = hash.toString(16);
if ((result.length() % 2) != 0) {
  result = "0" + result;
}

Please, note the check on the “toString()” result which must be padded with a zero if needed.

Have a nice hashing!

Similar Posts

Leave a Reply