简体   繁体   中英

php md5 hash equal to java

i just want working php md5 function(code) which generate equal result with below java code...

Normal php md5 function not giving equal result with this java code..

private static final String md5(String string) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(string.getBytes());
        byte[] arrby = messageDigest.digest();
        StringBuffer stringBuffer = new StringBuffer();
        int n = 0;
        block3 : do {
            if (n >= arrby.length) {
                return stringBuffer.toString();
            }
            String string2 = Integer.toHexString(255 & arrby[n]);
            do {
                String string3;
                if (string2.length() >= 2) {
                    stringBuffer.append(string2);
                    ++n;
                    continue block3;
                }
                string2 = string3 = "0" + string2;
            } while (true);

        } while (true);
    }
    catch (NoSuchAlgorithmException var1_7) {
        var1_7.printStackTrace();
return "";
    }
}

code edited...and example given....

String="fpwdxx-xx0xx-xx"+"8420474066"+"xx-xx"+"2.9"+"xx-xx"+"2016-09-14 21:13:25"+"xx-xx"+"AirTel"+"xx-xx"+""+"xx-xx"+"26b792f77e6f5157"+"xx-xx"+"911364603150705"+"xx-xx"+"A52+"+"xx-xx"+"4.2.2"+"z1t998044Fgrt...d";

Java Result: cfcb292130f3aa3cd49eed180e3d76a3

PHP Result: dd7e471b1a66a691097017c8de 2783a3

php has his own md5 function: http://php.net/manual/en/function.md5.php

md5($string)

this is standard implementation, but php pads the value with an extra 0 in front so it's always 16 characters, so you can check this

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