我正在尝试在我的 Node.js 网站上验证来自 WooCommerce 的 webhook 交易。但是我无法让 2 个字符串匹配。
可以看到php签名是用下面的代码生成的,源码可以看这里WooCommerce 来源 https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-webhook.php#L303-L317.
base64_encode( hash_hmac( $hash_algo, $payload, $this->get_secret(), true ) ));
我注意到如果我关掉true
on the hash_hmac
,然后我可以让 2 个系统创建匹配,但是我不想编辑 WooCommerce 的核心,所以我想知道这里是否缺少一些东西?
对于我的示例,我确实编辑了核心并强制有效负载如下,这样我就可以轻松尝试并匹配两个系统
payload = '{"id":1,"etc":2,"host":"http:/\/localhost\/view-order\/8"}'
secret = 'welcome'
我在 Node.Js 中的代码如下。
var crypto = require('crypto');
hmac = crypto.createHmac('sha256', secret);
hmac.setEncoding('binary');
hmac.write(payload);
hmac.end();
hash = hmac.read();
result = base64.encode(hash);
console.log(result);
If I从“host”JSON 中删除 url 然后它就可以工作了,这与它的转义方式有关吗?
我认为这可能是 PHP 和节点执行 SHA256 哈希的方式存在问题。我真的无法弄清楚如何解决这个问题。
任何帮助都会很棒,
Thanks