I have a use case where i have to generate md5
hash of a JSON
object and compare the hashes in the server and the browser.
The browser client generates hash and then asks the server for the hash of the same resource[ which happens to be a JSON object], and compares both the hashes to decide what to do next.
For server i am using Python
and browser client is in Javascript
.
For me the hashes generated in both cases do not match. Here's my code:
Python:
>>> import hashlib
>>> import json
>>> a = {"candidate" : 5, "data": 1}
>>> a = json.dumps(a, sort_keys = True).encode("utf-8")
>>> hashlib.md5(a).hexdigest()
>>> 12db79ee4a76db2f4fc48624140adc7e
JS: I am using md5 for hashing in browser
> var hash = require("md5")
> var data = {"candidate":5, "data":1}
> data = JSON.stringify(data)
> md5(data)
> 92e99f0a99ad2a3b5e02f717a2fb83c2
What is it that i am doing wrong?
Use the MD5 Algorithm in Python To obtain the hash value, use the digest() method, which returns a bytes object digest of the data fed to the hash object. Similar to the digest() method, you can also use hexdigest() , which returns a string object of the digest containing only hexadecimal digits.
The json. dumps method converts a Python object to a JSON formatted string. This works because strings are immutable and hashable.
You're assuming that both languages generate JSON that looks identical.
>>> json.dumps({"candidate" : 5, "data": 1}, sort_keys=True)
'{"candidate": 5, "data": 1}'
js> JSON.stringify({"candidate" : 5, "data": 1})
"{\"candidate\":5,\"data\":1}"
Fortunately, they can.
>>> a = json.dumps({"candidate" : 5, "data": 1}, sort_keys=True, indent=2)
'{\n "candidate": 5,\n "data": 1\n}'
js> var a = JSON.stringify({"candidate" : 5, "data": 1}, null, 2)
"{\n \"candidate\": 5,\n \"data\": 1\n}"
And now the hashes would be same as well.
Python:
>>> hashlib.md5(a.encode("utf-8")).hexdigest()
>>> d77982d217ec5a9bcbad5be9bee93027
JS:
>>> md5(a)
>>> d77982d217ec5a9bcbad5be9bee93027
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With