简体   繁体   中英

Convert Java MD5 `java.security.MessageDigest.getInstance(“MD5”)` code to Python2.7 using hashlib or other methods

Java code:

public static String md5(byte[] source) throws NoSuchAlgorithmException {
    char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
    md.update(source);
    byte tmp[] = md.digest();
    char str[] = new char[32];
    int k = 0;
    for (int i = 0; i < 16; i++) {
      str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
      str[k++] = hexDigits[tmp[i] & 0xf];
    }

    return new String(str);
  }

And that code, input:

77,48,48,47,48,48,47,48,48,47,114,66,65,72,104,108,120,121,80,107,113,65,89,90,78,106,65,65,67,102,97,85,116,51,77,89,52,55,53,46,106,112,101,103,49,49

return:

2fa040f96503fdabf5fd171da1473069

Then in python2.7, my code:

def md5(string):
    m = hashlib.md5()
    m.update(string)
    return m.hexdigest()

same input and return:

e480188fdf66deeec3d5a2c42094dd73

So how to edit my python code to have same return?

Taking a closer look at your input string 774848474848474848471146665721... it looks like a concatenated-together sequence of byte values representing ASCII characters:

77 48 48 47 48 48 47 48 48 47 114 66 65 72 ...

Converting these to characters we obtain the string M00/00/00/rBAHhlxyPkqAYZNjAACfaUt3MY475.jpeg11 .

Computing the MD5 hash of M00/00/00/rBAHhlxyPkqAYZNjAACfaUt3MY475.jpeg11 , I get 2fa040f96503fdabf5fd171da1473069 . This is the hash returned by your Java code.

On the other hand, computing the MD5 hash of the string 774848474848474848471146665721... returns e480188fdf66deeec3d5a2c42094dd73 . This is the hash returned by your Python code.

I have been able to calculate both of these hashes using your Java code and your Python code, and get the same results in both. To convert a string to a byte array in Java I called .getBytes("UTF-8") on the string. (I used Python 3 rather than Python 2, and so had to insert a call to .encode("utf-8") , but that's an aside.)

Therefore, your Java and Python code samples for computing MD5 hashes are returning different results because you are passing them different input data.

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