I am trying to port a python program to c#. Here is the line that’s supposed to be a walkthrough but is currently tormenting me:
hash = hashlib.md5(inputstring).digest()
After generating a similar MD5 hash in c# It is absolutely vital that I create a similar hash string as the original python program or my whole application will fail.
My confusion lies in which encoding to use when converting to string in c# i.e
?Encoding enc = new ?Encoding();
string Hash =enc.GetString(HashBytes); //HashBytes is my generated hash
Because I am unable to create two similar hashes when using Encoding.Default i.e
string Hash = Encoding.Default.GetString(HashBytes);
So I’m thinking knowing the deafult hash.digest() encoding for python would help
EDIT
Ok maybe some more code will articulate my problem more. After the hash is calculated in the python program some calculations are carried out i.e
hash = hashlib.md5(inputstring).digest()
for i in range(0,6):
value += ord(hash[i])
return value
Now can you see why two different Hash strings will be problematic? Some of the characters that appear when the python program is ran are repalced by a ‘?’ in C#.
I presume you’re using an earlier version of Python than 3, and your string is a normal
str.If you’re talking about the output, the digest method returns a string consisting on raw bytes . The equivalent type in C# is
byte[], which you already seem to have. It’s not text, so using the Encoding class makes no sense.If you’re talking about the input, the
md5function takes in a normalstr, which is a string of bytes. You’ll have to look at the code before that to figure out what encoding the data is in.Edit:
Regarding the code you posted, all it’s doing is it’s taking the values of the six first bytes in the hash and adding them together. You should be able to figure out how to do that in C#.
And make sure you learn the difference between a string of bytes and a string of characters.