【发布时间】:2016-05-30 21:49:52
【问题描述】:
我正在尝试验证传入的 webhook,但目前生成的哈希与 api 生成的测试哈希不匹配。
文档列出了以下 Ruby 示例,但我使用的是 Python/Django,因此任何“转换”此函数的帮助将不胜感激!
Ruby 函数
# request_signature - the signature sent in Webhook-Signature
# request_body - the JSON body of the webhook request
# secret - the secret for the webhook endpoint
require "openssl"
digest = OpenSSL::Digest.new("sha256")
calculated_signature = OpenSSL::HMAC.hexdigest(digest, secret, request_body)
if calculated_signature == request_signature
# Signature ok!
else
# Invalid signature. Ignore the webhook and return 498 Token Invalid
end
到目前为止,这大致是我自己使用https://docs.python.org/3/library/hashlib.html 整理出来的。
Python 尝试
import hashlib
secret = "xxxxxxxxxxxxxxxxxx"
json_data = {json data}
h = hashlib.new('sha256')
h.update(secret)
h.update(str(json_data))
calculated_signature = h.hexdigest()
if calculated_signature == webhook_signature:
do_something()
else:
return 498
当我运行上述代码时,由于我的 Python 实现不正确,哈希值明显不匹配。
任何帮助/指针将不胜感激!
【问题讨论】:
标签: python django openssl webhooks