我已經(jīng)獲得了幾行 JavaScript 代碼,我需要將其轉(zhuǎn)換為等效的 PHP 代碼。作為一名 JavaScript 開發(fā)人員,我正在為此苦苦掙扎。這是我提供的 JavaScript 示例:const crypto = require('crypto')const secret_in_hex = Buffer.from(secret, 'hex');const hash = crypto.createHmac('sha512', secret_in_hex) .update(body) .digest('hex')// Compare hash with the received X-Onfleet-Signature in raw bytes我正在使用 PHP 設(shè)置 Webhook 接收器的 API 文檔提到:Each webhook request contains a signature from Onfleet in X-Onfleet-Signature header. To authenticate the webhook request received on your webhook server, you will need to validate against this header. To validate against X-Onfleet-Signature, you will need to compare its value with an HMAC you have generated using the hexadecimal format of your webhook secrets and the full body of the webhook POST request in raw bytes.我假設(shè)我將使用該hash_hmac函數(shù),并且可能會使用該bin2hex函數(shù),但此時完全被難住了,如果有人可以向我展示與上述 JavaScript 等效的 PHP(假設(shè)有一個),我將不勝感激。
1 回答

湖上湖
TA貢獻2003條經(jīng)驗 獲得超2個贊
最簡單的等價應(yīng)該是:
$secretInHex?=?hex2bin($secret); $hash?=?hash_hmac('sha512',?$body,?$secretInHex);
您應(yīng)該注意,hex2bin函數(shù)不會將十六進制數(shù)轉(zhuǎn)換為二進制數(shù),但它會解碼十六進制編碼的二進制字符串。
所以提供的秘密應(yīng)該已經(jīng)是十六進制,否則hex2bin
會拋出異常
- 1 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報
0/150
提交
取消