【问题标题】:Build hmac sha256 hashing algorithm in node.js在 node.js 中构建 hmac sha256 哈希算法
【发布时间】:2020-11-27 05:16:55
【问题描述】:

我正在使用Bold Commerce Webhooks 订阅我商店中的订阅事件。他们提供了有关如何在 PHP 中生成请求签名的文档:

$now = time(); // current unix timestamp
$json = json_encode($payload, JSON_FORCE_OBJECT);
$signature = hash_hmac('sha256', $now.'.'.$json, $signingKey);

我正在尝试在 node.js 中重新创建我这边的哈希。根据我的研究,到目前为止,我已经得出以下结论,我认为这非常接近,但还不匹配:

const hash = request.header("X-Bold-Signature")!;

const SECRET = "my-secret-api-key";
const body = request.body;
const time = request.header("timestamp")!;

const mySignature = crypto.createHmac('sha256', SECRET).update(time + '.' + body).digest("hex");

if (mySignature !== request.header("X-Bold-Signature")!) {
    //...
}

我也尝试过使用 JSON.stringify(body) 更改哈希但仍然不匹配。

【问题讨论】:

    标签: node.js http-headers sha256 hmac cryptojs


    【解决方案1】:

    它与此代码匹配。

    const hash = crypto
      .createHmac('sha256', secretKey)
      .update(timestamp + '.' + JSON.stringify(body))
      .digest('hex');
    

    请注意,与 shopify 不同,不要使用 rawbody。

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 2013-05-12
      • 2016-08-20
      • 2014-01-14
      • 2016-10-10
      • 2019-03-09
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多