php hmac sha512 签名不是 working/different 然后在 python

php hmac sha512 signature not working/different then in python

我找不到签名密钥不同的问题。我认为它与python有关,它需要有字节用于输入。但是两种语言的输出是完全不同的。不知道有什么问题。

API_SECRET = 'ErLN2dewKAfY5j4bHOIFQaCKogJIv8Tq'

php

$digest2 = [
'486',
'GET',
'/orderlist',
'2.0',
'2020-01-29T11:45:47+01:00'];

print_r(base64_encode(hash_hmac('sha512', implode('', $digest2), $this->_API_SECRET, true)));

它会给出这个代码:

BsPgLClFDYl6oMRXkph7sz3opLeRAenzZ1w1X5fhiyDC4WVZViTvIWnLZWb1lQDVCRZ2/MkPT8irU9gax3m2Iw==

但是当我试图在 python 中获取相同的代码时:

python

digest2 =['486','GET','/orderlist','2.0','2020-01-29T11:45:47+01:00']
#token = hmac.new(bytes('API_SECRET'.encode("utf-8")), msg=bytes(''.join(digest2 ).encode("utf-8")), digestmod=hashlib.sha512).hexdigest()
digest = hmac.new(bytes('API_SECRET'.encode("utf-8")), bytes(''.join(digest2 ),"utf-8"), digestmod=hashlib.sha512).hexdigest()
return base64.b64encode(digest.encode("utf-8"))

它会给出这个代码:

b'YThhNGI0ZGZkN2Q5YmJmNDg5OTJmNzE1NGZiMWQyMWExYzQyODU3OGUxNmQ1ZTRkZWUxOGViZTUxNmFhM2M4MGZmZTYzMTJhNWNhYWQ5MDg3NDdlMGQ0NDBmMDliZThkNTA2ODA1YTlkMjJmZGMwZTk3NjVkNmM2MmE0NTViMjQ=

您对十六进制字符串使用了 base64 编码。您还使用 API_SECRET 作为字符串而不是变量。这是工作解决方案

import hashlib
import hmac
import base64

API_SECRET = "ErLN2dewKAfY5j4bHOIFQaCKogJIv8Tq"
digest2 = ["486", "GET", "/orderlist", "2.0", "2020-01-29T11:45:47+01:00"]
digest = hmac.new(
    API_SECRET.encode("utf-8"),
    "".join(digest2).encode("utf-8"),
    digestmod=hashlib.sha512,
).digest()
print(base64.b64encode(digest))

版画

'BsPgLClFDYl6oMRXkph7sz3opLeRAenzZ1w1X5fhiyDC4WVZViTvIWnLZWb1lQDVCRZ2/MkPT8irU9gax3m2Iw=='