python-3.x - 在 python 中生成签名值

标签 python-3.x sha256

所以我正在看的文档说用 C# 生成一个签名值,但是我想在 Python 中完成整个事情这是他们给我的信息,但是预期的结果是签名:wpGKFbhcBl+8JLVXGP0QqBooK6dtLBv9bYtI15NXL1U=对于 SHA256,我要返回 b'YzI5MThhMTViODVjMDY1ZmJjMjRiNTU3MThmZDEwYTgxYTI4MmJhNzZkMmMxYmZkNmQ4YjQ4ZDc5MzU3MmY1NQ=='

有人能指出我正确的方向吗?

var bytesToHash = Encoding.UTF8.GetBytes(TimeStamp.GetUnixTime() + transactionId + apiKey);
string signature = Convert.ToBase64String(GetHashAlgorithm(Algorithm).ComputeHash(bytesToHash));

我的代码:

import hashlib
import base64
epochTime = 1547498533216
transactionId = "07643622"
APIKey = "13f1fd1b-ab2d-4c1f-8e2c-ca61878f2a44"
hash256 = bytes(str(epochTime) + transactionId + APIKey,'utf-8')
print(hash256)
signature = base64.b64encode(bytes(hashlib.sha256(hash256).hexdigest(),'utf-8'))
print(signature)

最佳答案

您唯一的问题是您对 hexdigest(一种将每个字节显示为 0-9a-f 范围内的两个字符的字符串形式进行编码,这将用于作为 base64 的替代方法来制作人类可读的哈希值,而不是与它结合使用),而不是 digest(SHA256 哈希值的原始字节)。变化:

signature = base64.b64encode(bytes(hashlib.sha256(hash256).hexdigest(),'utf-8'))

到:

signature = base64.b64encode(hashlib.sha256(hash256).digest())

并且输出如您所料:b'wpGKFbhcBl+8JLVXGP0QqBooK6dtLBv9bYtI15NXL1U='

关于python-3.x - 在 python 中生成签名值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63878296/

相关文章:

python - 根据另一个字典的值过滤字典列表

python - Selenium 错误 - ConnectionResetError : [WinError 10054] An existing connection was forcibly closed by the remote host

freebsd - 哪里可以找到 FreeBSD sha256 实用程序源代码

python - Flask 应用程序因多个进程而锁定

Python 质数 for-else 范围

python - 如何使用美国农业部 API

cryptography - SHA 256伪代码?

python - Python 2.4 中的 SHA256 哈希

rust - 如何将 crypto::sha2::Sha256 哈希转换为 &[u8] 表示形式?

c# - 密码哈希 - 为什么加盐 60,000 次