I try to bring some code from JS to Python. I'm stuck with that code in JS :
const crypto = require('crypto')
var txtToHash = "Hello¤World¤";
var md5sum = crypto.createHash('md5');
md5sum.update(new Buffer(txtToHash, 'binary'));
md5val = md5sum.digest('hex');
// equivalent to
// crypto.createHash('md5').update(urlPart, 'binary').digest('hex'));
Returns :
3a091f847ee21c7c1927c19e0f29a28b
And, in Python 3.7 I have this code :
import hashlib
txtToHash = "Hello¤World¤"
md5val = hashlib.md5(txtToHash.encode()).hexdigest()
Returns : f0aef2e2e25ddf71473aa148b191dd70
Why are they different please ? I can't find an answer on Google or SO.
you are using two different character encodings during digest creation.
Make sure that you have the same type of character encoding. your node js implementation is using 'binary' alias 'latin1' encoding. Where as the code in python uses UTf8 character encoding.
When you specified txtToHash.encode() , this means that encode the text to utf-8.
So modify your digest creation to match the character encoding the same on both the environments.
either modify your nodejs code
md5sum.update(new Buffer(txtToHash, 'utf8'));
or modify your python code to
md5val = hashlib.md5(txtToHash.encode('latin1')).hexdigest()
the above should give the same result >> 3a091f847ee21c7c1927c19e0f29a28b
Note: Although the python code gives the desired result. I wouldn't suggest this because latin1 encoding has only a small subset of characters compared to utf8. So I recommend that you change the encoding to utf-8 in your node js app and apply the same encoding in python too