Integer.toHexString in Python

0

Here I have java code

static String md5(byte[] bArr) {
    MessageDigest instance = MessageDigest.getInstance("MD5");
    instance.update(bArr);
    byte[] digest = instance.digest();
    StringBuffer sb = new StringBuffer();
    for (byte b : digest) {
        sb.append(Integer.toHexString(b & 0xffffffff));
    }
    return sb.toString();
}

calling md5("A".getBytes()), I got

7fffffffc56270ffffffe7ffffffa7fffffffa81a5935ffffffb72effffffacffffffbe29

How can I achieve the same result by Python? I tried to use the following

m = hashlib.md5()
m.update(str.encode('utf-8'))
bytes = m.digest()
return ''.join('{:02x}'.format(x) for x in bytes)

I got

7fc56270e7a70fa81a5935b72eacbe29

which is the same with m.hexdigest()

Edit: The issue looks with those "negative" bytes in between. e.g second byte is "-59" in int in Java but Python doesn't somehow recognize and 5th byte is "-25"

Those 'f's are because these are negative.

python
asked on Stack Overflow Aug 3, 2020 by user700517 • edited Aug 3, 2020 by Vishesh Mangla

1 Answer

0

The java version is... weird. It takes every byte, widens it to an integer, then takes the one's complement of the integer and formats that. It's like a broken version of making signed bytes unsigned.

Which is completely unnecessary in the first place in Python since bytes are always unsigned. But if you really want it, you can just format x | 0xffffff00 to hex (it's going to "pad" the byte with 6 ffs).

answered on Stack Overflow Aug 3, 2020 by Masklinn

User contributions licensed under CC BY-SA 3.0