What is the equivalent of PHP's md5(str, true)?

1.7k views Asked by At

I am calculating the MD5 in PHP with following line (see documentation for more info):

md5($password, true); // returns raw output

I am using the following Java code:

byte[] bytesOfMessage = password.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);

Above code doesn't return the same output as the one returned by the PHP code.

How can I solve it for Android/Java generate exactly the same MD5 with raw output not hash string?

2

There are 2 answers

0
Alejandro Bentivengo On

I had the same issue, this is my Java program code:

public static String encryptPassword(String password) {
    String hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes("UTF-8"));
        byte[] raw = md.digest();
        hash = (new BASE64Encoder()).encode(raw);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return hash;
}

And this is my php code:

<?php
  $str = 'encodeIt';
  $toutf8 = utf8_encode($str);
  $var = md5($str,true);
  echo base64_encode($var);
?>

They return always the same hash.

6
Rahim Rahimov On

Yes. I had the same problem. I find the right way:

That is my equivalent of PHP's md5(password, true):

 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

 public byte[] md5x16(String text) {
    try {
        MessageDigest digester = MessageDigest.getInstance("MD5");
        digester.update(text.getBytes());
        byte[] md5Bytes = digester.digest();
        String md5Text = new String(md5Bytes); // if you need in String format
        // better use md5Bytes if applying further processing to the generated md5.
        // Otherwise it may give undesired results.
        return md5Bytes;

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

and equivalent of PHP's md5(password, false):

public static String md5(String text) {
    try {
        MessageDigest digester = MessageDigest.getInstance("MD5");
        digester.update(text.getBytes());
        byte[] md5Bytes = digester.digest();
        String md5Text = null;

        md5Text = bytesToHex(md5Bytes);

        return md5Text;

    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

And if you need to convert an equivalent of PHP's base64_encode(text), use this one:

public String convertToBase64(byte[] bytes) {
    try {
        String base64 = Base64.encodeToString(bytes, Base64.DEFAULT);
        return base64;
    }
    catch (Exception e) {
    }
    return "";
}