起きている問題
PythonでPHP関数のhash_equals
に当たるような関数をご存知の方はいますでしょうか?
やりたいこと
やりたいこととしましては、shopify webhookの以下のソースをPythonに訳あって書き直したいです。
php
1<?php 2 3define('SHOPIFY_APP_SECRET', 'my_shared_secret'); 4 5function verify_webhook($data, $hmac_header) 6{ 7 $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true)); 8 return hash_equals($hmac_header, $calculated_hmac); 9} 10 11 12$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']; 13$data = file_get_contents('php://input'); 14$verified = verify_webhook($data, $hmac_header); 15error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result 16 17?>
試したこと
Pythonで以下のように書いてみました。
python
1import json 2import base64 3import hmac 4import hashlib 5 6SHARED_SECRET = 'my_shared' 7 8def verify_webhook(data, hmac_header): 9 calculated_hmac = base64.b64encode(hmac.new(SHARED_SECRET, data, hashlib.sha256).hexdigest()) 10 # ここで比較を行う必要がある 11 12 13def lambda_handler(event, context): 14 hmac_header = event['params']['header']['X-Shopify-Hmac-Sha256'] 15 verified = verify_webhook(data, hmac_header)
PHPでhash_equals
と書かれている部分をPythonでどう実装したらいいかわかりません。
ご教授お願いできますでしょうか
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。