首页 > 解决方案 > 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()

但它也不起作用。

任何建议将被认真考虑。

标签: pythonhmac

解决方案


推荐阅读