简体   繁体   English

生成 JSON 的 md5 哈希并在 Python 和 JavaScript 中进行比较

[英]Generate md5 hash of JSON and compare in Python and JavaScript

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.我有一个用例,我必须生成JSON对象的md5哈希并比较服务器和浏览器中的哈希。

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.浏览器客户端生成哈希,然后向服务器请求相同资源的哈希[恰好是一个 JSON 对象],然后比较两个哈希以决定下一步做什么。

For server i am using Python and browser client is in Javascript .对于服务器,我使用Python ,浏览器客户端使用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 JS :我在浏览器中使用md5进行散列

> 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?我做错了什么?

You're assuming that both languages generate JSON that looks identical.您假设两种语言都生成看起来相同的 JSON。

>>> 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: Python:

>>> hashlib.md5(a.encode("utf-8")).hexdigest()
>>> d77982d217ec5a9bcbad5be9bee93027

JS: JS:

>>> md5(a)
>>> d77982d217ec5a9bcbad5be9bee93027

The difference is that json.dumps applies some minor pretty-printing by default but JSON.stringify does not, that's why hashes are not the same.不同之处在于json.dumps应用了一些小的漂亮打印,但JSON.stringify没有,这就是哈希不一样的原因。
Python: Python:

 >>> import json
 >>> json.dumps({"candidate" : 5, "data": 1})
     '{"candidate": 5, "data": 1}'

Javacript: Java脚本:

 > JSON.stringify({"candidate" : 5, "data": 1})
   '{"candidate":5,"data":1}'

But with some modification, we can generate the same hash.但是通过一些修改,我们可以生成相同的哈希。 There are two ways for it:-有两种方法:-

  1. Modifying javascript JSON string to make it equivalent to a python JSON string .修改javascript JSON string以使其等效于python JSON string
    Python: Python:
     >>> import json,hashlib >>> a = json.dumps({"candidate" : 5, "data": 1}, sort_keys=True) >>> hashlib.md5(a.encode("utf-8")).hexdigest() '12db79ee4a76db2f4fc48624140adc7e'
    Javacript: Java脚本:
     > const Crypto = require("crypto-js") undefined > const a = JSON.stringify({"candidate" : 5, "data": 1}).replaceAll(":", ": ").replaceAll(",", ", ") undefined > Crypto.MD5(a).toString(Crypto.enc.Hex) '12db79ee4a76db2f4fc48624140adc7e'
  2. Modifying python JSON string to make it equivalent to a javascript JSON string .修改python JSON string以使其等效于javascript JSON string
    Python: Python:
     >>> import json,hashlib >>> a = json.dumps({"candidate" : 5, "data": 1}, separators=(',', ':')) >>> hashlib.md5(a.encode("utf-8")).hexdigest() '92e99f0a99ad2a3b5e02f717a2fb83c2'
    Javacript: Java脚本:
     > const Crypto = require("crypto-js") undefined > const a = JSON.stringify({"candidate" : 5, "data": 1}) undefined > Crypto.MD5(a).toString(Crypto.enc.Hex) '92e99f0a99ad2a3b5e02f717a2fb83c2'

    Note:- To run javascript code, crypto-js npm pkg should be installed as same location where you started the node shell.注意:- 要运行 javascript 代码,crypto-js npm pkg 应该安装在您启动节点 shell 的相同位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM