我已經獲得了幾行 JavaScript 代碼,我需要將其轉換為等效的 PHP 代碼。作為一名 JavaScript 開發人員,我正在為此苦苦掙扎。這是我提供的 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 設置 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.我假設我將使用該hash_hmac函數,并且可能會使用該bin2hex函數,但此時完全被難住了,如果有人可以向我展示與上述 JavaScript 等效的 PHP(假設有一個),我將不勝感激。
1 回答

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