简体   繁体   中英

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

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?

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 "";
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM