-2

It seems that MD5Digest in BouncyCastle and hashlib.md5 in Python produce different results.

Why is that?

C#:

static string Hash(string str)
{
    var utf = System.Text.Encoding.UTF8.GetBytes(str);
    var md5 = new Org.BouncyCastle.Crypto.Digests.MD5Digest();
    var hash = new byte[16];
    md5.BlockUpdate(utf, 0, utf.Length);
    md5.Finish();
    md5.DoFinal(hash, 0);
    return Convert.ToBase64String(hash);
}
Console.WriteLine(Hash("MRJAf16c0eda-c449-4d5d-8250-faa2b2c739d8.zipPHMD"));
// jHaqLPD7eECs5yRj930EYA==

Python:

import hashlib, base64
def Hash(str):
    utf = str.encode("utf-8")
    md5 = hashlib.md5()
    md5.update(utf)
    hash = md5.digest()
    return base64.b64encode(hash).decode()
print(Hash("MRJAf16c0eda-c449-4d5d-8250-faa2b2c739d8.zipPHMD"))
# +0soCxXP9qWSUFz/oJ8I9Q==

Even though the hashes should match.

  • Please show the resulting hashes. As the usual hex, like `fb4b280b15cff6a592505cffa09f08f5`. Not further processed with base64. – Kelly Bundy Sep 02 '23 at 23:01
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 03 '23 at 04:25
  • The Python hash is correct. Remove the line `md5.Finish()` in the C#/BC code, then the C#/BC code returns the same hash. – Topaco Sep 03 '23 at 05:32

0 Answers0