簡體   English   中英

PHP - 使用十六進制格式使用 HMAC 進行 Webhook 身份驗證

[英]PHP - Webhooks Authentication with HMAC using the hexadecimal format

我正在研究與 Onfleet API 配合使用的 PHP Webhook 接收器。 對於Webhook 身份驗證,它們需要以下內容:

每個 webhook 請求都包含來自 X-Onfleet-Signature header 中的 Onfleet 的簽名。 要驗證在您的 webhook 服務器上收到的 webhook 請求,您需要針對此 header 進行驗證。 要針對 X-Onfleet-Signature 進行驗證,您需要將其值與您使用 webhook 機密的十六進制格式生成的 HMAC 和原始字節的 webhook POST 請求的完整主體進行比較。

我從來沒有使用過十六進制格式和原始字節等。我的方法是使用像這樣使用 base64 編碼的東西,看看我是否可以適應它,因為它應該非常接近希望:

$myWebhookSecret = 'abc123';

$payload = file_get_contents("php://input");

$yourHash = base64_encode(hash_hmac('sha512', $payload, $myWebhookSecret, true));

$onfleetSignature = $_SERVER['X-Onfleet-Signature'];    

if (hash_equals($onfleetSignature, $yourHash)) {
    $result = 'Success';
    http_response_code(200);
    
} else {
    $result = 'Failure';
    http_response_code(401);
    die;
}

我希望我只需要轉換這一行:

$yourHash = base64_encode(hash_hmac('sha512', $payload, $myWebhookSecret, true));

在這里使用十六進制格式,但不確定 PHP 是否可以這樣做或如何?

這最終對我有用:

$myWebhookSecret = 'abc123';

$payload = file_get_contents("php://input");

$secretInHex = hex2bin($myWebhookSecret);
$yourHash = hash_hmac('sha512', $payload, $secretInHex);

$onfleetSignature = $_SERVER['X-Onfleet-Signature'];    

if (hash_equals($onfleetSignature, $yourHash)) {
    $result = 'Success';
    http_response_code(200);

} else {
    $result = 'Failure';
    http_response_code(401);
    die;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM