Python body.encode()Javascript替代方案

Python body.encode()Javascript替代方案,javascript,python,node.js,sha256,plaid,Javascript,Python,Node.js,Sha256,Plaid,我试图通过计算webhook主体的Sha256来验证来自NodeJS中Plaid的webhook,下面是一段Python代码,代码如下所示: # Compute the has of the body. m = hashlib.sha256() m.update(body.encode()) body_hash = m.hexdigest() 在将body.encode()传递给Sha256函数之前,Javascript中的body.encode()的替代方法是什么?

我试图通过计算webhook主体的Sha256来验证来自NodeJS中Plaid的webhook,下面是一段Python代码,代码如下所示:

   # Compute the has of the body.
   m = hashlib.sha256()
   m.update(body.encode())
   body_hash = m.hexdigest() 
在将body.encode()传递给Sha256函数之前,Javascript中的body.encode()的替代方法是什么?请注意,我得到的主体是一个包含以下数据的对象:

{错误:null,项目id:'4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
新的\u事务:0,webhook\u代码:“默认\u更新”,webhook\u类型: “事务”}

然而,我试图得到这个散列:

B05EF560B59E8D8E427433C5E0F6A11579B5DFE653257558B896F858007385A


因此,如果主体是JSON(不是JSON字符串),则需要将其字符串化,并将其放入
.update
函数中,因为
m.body
接受一个字符串。如果你的身体像一根绳子,那么就按原样把它放进去

这来自加密示例:

编辑:
如果散列不相同,那么可能需要更正换行符或空格。你需要使两个物体完全相同。在下面的示例中,我使用相同的字符串,并使用Python和NodeJS进行编码

import hashlib
body = '{"error":null,"item_id":"4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG","new_transactions":0,"webhook_code":"DEFAULT_UPDATE","webhook_type":"TRANSACTIONS"}'
m = hashlib.sha256()
m.update(body.encode())
body_hash = m.hexdigest()
print(body_hash)
输出:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
并使用NodeJS:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
输出:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba

因此,如果主体是JSON(不是JSON字符串),则需要将其字符串化,并将其放入
.update
函数中,因为
m.body
接受一个字符串。如果你的身体像一根绳子,那么就按原样把它放进去

这来自加密示例:

编辑:
如果散列不相同,那么可能需要更正换行符或空格。你需要使两个物体完全相同。在下面的示例中,我使用相同的字符串,并使用Python和NodeJS进行编码

import hashlib
body = '{"error":null,"item_id":"4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG","new_transactions":0,"webhook_code":"DEFAULT_UPDATE","webhook_type":"TRANSACTIONS"}'
m = hashlib.sha256()
m.update(body.encode())
body_hash = m.hexdigest()
print(body_hash)
输出:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
并使用NodeJS:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
输出:

python3 file.py
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const body = {
  error: null,
  item_id: '4zAGyokJ1XiWP63QNl1RuLZV76o55nudVXzNG',
  new_transactions: 0,
  webhook_code: 'DEFAULT_UPDATE',
  webhook_type: 'TRANSACTIONS'
}

const stringBody = JSON.stringify(body);
hash.update(stringBody);
console.log(hash.digest('hex'));
node file.js
26f1120ccaf99a383b7462b233e18994d0c06d4585e3fe9a91a449e97a1c03ba

您应该散列webhook的主体字节,而不是从JSON解码的对象。你已经有了一个“对象”,所以你做得太过分了。请问如何获取webhook的正文字节?如果没有看到任何代码,很难说。你应该散列webhook的正文字节,而不是你从JSON解码的对象。你已经有了一个“对象”,所以你做得太过分了。请问如何获取webhook的正文字节?如果没有看到任何代码,很难说。感谢你的回答,但即使使用JSON.stringify(正文)我也没有得到相同的答案来添加比较。感谢你的回答,但即使使用JSON.stringify(正文)我没有得到相同的答案来添加比较。