python - hmac 将不同的 hexdigest 值返回给 openssl
问题描述
例如,当我在终端中运行此命令时:
echo -n 'something' | openssl dgst -sha256 -hmac 'NhqPtmdS'
这是返回的:
caa686a03a502a0da2985dfea0b0b5798657fc30c2fd917db527d29ea5b23579
我正在尝试在 Python 中执行此操作,但我不知道为什么会返回不同的东西。
这是我的代码:
import base64
from hashlib import sha256
import hmac
key = base64.b64decode('NhqPtmdS')
jsonBytes = bytes('something', "ascii")
hmac_result = hmac.new(key, jsonBytes, sha256).hexdigest()
print(hmac_result)
但我得到了下一个结果:
6a964bd560a9dc763864ddf337d64e5f2ef958e6937ad296084166da0db83eb9
我也试过这个:
hmac_result = hmac.new(key, jsonBytes, sha256)
base64.b64encode(hmac_result.digest()).decode()
但它也不起作用。
任何建议将被认真考虑。
解决方案
推荐阅读
- python - 使用 for 循环调用递归
- python - 如何从 seaborn 的集群图中的空颜色栏/cbar 中删除大空白?
- java - Java-Swing:检索和更新数据库中的详细信息
- java - 我将如何用 Java 编写这个 C++ 输入函数?
- python - 数据帧模式功能
- java - 试图将嵌套的 for 循环表示为 Java 流,但感到困惑
- javascript - 如何更改对象中的布尔值?
- ruby-on-rails - Rails 应用程序在部署到 Azure 时崩溃
- javascript - 如何根据导入组件的页面更改 GatsbyJS 组件内的元素的背景颜色?
- java - 在 Swift 中解密 Java RSA/ECB/PKCS1Padding 加密字符串时出现问题