[英]base64 HMAC with SHA256 in Python
I am having a hard time creating a signature.我很难创建签名。
I am needing to make a signature using HMAC with SHA256 using a Checkout Request JSON and a secret key.我需要使用带有 SHA256 的 HMAC 使用 Checkout Request JSON 和密钥进行签名。 I need to do it by concatenating signature, pipe character (|) and Checkout Request JSON and then encoding it with BASE64.
我需要通过连接签名、pipe 字符 (|) 和 Checkout Request JSON 然后用 BASE64 对其进行编码来做到这一点。
This is a formula I found in the documentations:这是我在文档中找到的公式:
$signed_checkout_request = base64( hmac_sha256( $checkout_request, $private_key ) + "|" + $checkout_request )
I have made this based on some online code:我是根据一些在线代码制作的:
import hashlib
import hmac
import base64
checkout_request = '{"charge":{"amount":499,"currency":"EUR"}}'.encode('utf-8');
private_key = b'44444444444';
digest = hmac.new(private_key, msg=checkout_request, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()
However I am not sure how to get the "|"但是我不确定如何获得“|” into it.
进去。 I am also not sure if I am even on the right track if I am honest... I don't have much experience in this section and I have failed at googling.
老实说,我也不确定我是否走在正确的轨道上……我在这部分没有太多经验,而且我在谷歌搜索上也失败了。
private_key = 'blahblahblah'
checkout_request = json.dumps({"charge":{"amount":4999,"currency":"EUR"}}, sort_keys=True, separators=(",", ":"))
digest = hmac.new(private_key.encode(), msg=checkout_request.encode(), digestmod=hashlib.sha256,).hexdigest()
signature = base64.b64encode((digest + "|" + checkout_request).encode()).decode()
I was able to get it to work with that:)我能够让它与它一起工作:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.