【问题标题】:attempting to verify a webhook secret with hmac and sha256尝试使用 hmac 和 sha256 验证 webhook 机密
【发布时间】:2023-03-11 08:20:01
【问题描述】:

所以我在这里有点困惑。

我正在使用一个将 webhook 发送到我的服务器的 api。它允许我们验证请求是否来自 api。

我正在使用 django rest 框架。

签名是这样过来的

Mux-Signature: t=1565220904,v1=20c75c1180c701ee8a796e81507cfd5c932fc17cf63a4a55566fd38da3a2d3d2 t= 之后的值是时间戳。 v1=之后的值就是签名

muxsecurityheader = request.META['HTTP_MUX_SIGNATURE']
        spiltmuxsecurityheader = muxsecurityheader.split(',')
        timestamp = spiltmuxsecurityheader[0]
        timestamp = timestamp[2:]
        receviedsignature = spiltmuxsecurityheader[1]
        receviedsignature = receviedsignature[3:]
        secretkey = 'a string that is my key'
        payload = timestamp + '.' + str(request.body)
        expectedsignature = hmac.new(secretkey, bytes(payload, 'UTF-8'), hashlib.sha256).hexdigest()
        print(expectedsignature)

是我目前的代码。

在 apis 文档 https://docs-legacy.mux.com/docs/webhook-security 的示例中

它声明通过提供的示例确定预期的签名。显然这只是伪代码

secret = // your signing secret
payload = timestamp + "." + request_body
expected_signature = createHmacSha256(payload, secret)

这与我上面的代码相似。

但是我遇到了一些错误并且

expectedsignature = hmac.new(secretkey, bytes(payload, 'UTF-8'), hashlib.sha256).hexdigest()
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'

就是其中之一。

例如,我正在尝试解决一个问题。而且我正处于你们中的一个人将立即看到我做错了什么的地步。非常感谢您提供一些清晰的说明。

【问题讨论】:

    标签: signature sha256 hmac


    【解决方案1】:

    这里是解决方案。我需要把东西变成字节!

    muxsecurityheader = request.META['HTTP_MUX_SIGNATURE']
            spiltmuxsecurityheader = muxsecurityheader.split(',')
            timestamp = spiltmuxsecurityheader[0]
            timestamp = timestamp[2:]
            receviedsignature = spiltmuxsecurityheader[1]
            receviedsignature = receviedsignature[3:]
            secretkey = 'the secret key that they gave me and stuff'
            payload = bytes(timestamp, "UTF-8") + bytes('.','UTF-8') + request.body
            expectedsignature = hmac.new(bytes(secretkey, 'UTF-8'), payload, hashlib.sha256).hexdigest()
            if expectedsignature == receviedsignature:
                print('yay')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多