The byte string is the same in python and C #, but hashcode md5 is different

I want to generate a hash code from a string, for example, "咖啡", but the hash code that I get from python and C # is different from what I want

WITH#

String str = "咖啡";
MD5 m = MD5.Create();
byte[] data = m.ComputeHash(Encoding.Default.GetBytes(str));
StringBuilder sbuilder = new StringBuilder();
for(int i=0;i<data.Length;i++){
  sbuilder.Append(data[i].ToString("x2"));
}
byte[] hex = Encoding.Default.GetBytes(str);
StringBuilder hex_builder = new StringBuilder();
foreach(byte a in hex){
  hex_builder.Append("{0:x2}",a);
}
//md5 hash code
Response.Write(sbuilder.ToString());
//binary string
Response.Write(hex_builder.ToString());

Python

#coding:utf8
str = '咖啡'
m = hashlib.md5()
m.update(str)
#md5 hashcode
print m.hexdigest()
#binary string
print ' '.join(["%02x"%ord(x) for x in str])

binary string - e5 92 96 e5 95 a1 in both C # and python

hash code md5 :

(C #) a761914f9760af3c112e24f08dea1b16

(Python) 3b7daa58a1fecdf5ba4d94d539fbb4d5

+3
source share
2 answers

String encoding may vary; so when converting a string to byte [], you probably get different values. Try printing them to make sure they are the same.

+5
source

. "UTF-16LE", # Python .

def getMD5(text):
    encoding = text.encode("UTF-16LE")
    md5 = hashlib.md5()
    md5.update(encoding)
    return md5.hexdigest()
+3

All Articles