How to Make Java MD5 Match PHP and MySQL MD5

Java MySQL PHP

Here is Java implementation of MD5 hashing that will produce exactly the same result as md5() function in PHP and MySQL:

public static String md5(String input) throws NoSuchAlgorithmException {
    String result = input;
    if(input != null) {
        MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
        md.update(input.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        result = hash.toString(16);
        while(result.length() < 32) { //40 for SHA-1
            result = "0" + result;
        }
    }
    return result;
}
Feb 11, 2010
profile for serg at Stack Overflow, Q&A for professional and enthusiast programmers