python - Bash 与 Python 中视觉相似字符串的 SHA256 有所不同

标签 python bash utf-8 ascii aws-sts

我正在尝试构建一个 AWS Signature v4 Auth header 来调用 STS GetCallerIdentity API 根据文档 https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html在 Bash 中。

现在,我在 Python 中执行相同的流程,在仔细研究 Python 和 Bash 中的脚本和输出后,我发现 Bash 中计算的字符串 SHA256 与 Python 中计算的不同。

这些字符串在我的文本编辑器中看起来是一样的——逐个字符。

但是由于 SHA256 不同,我假设这很大程度上是字符串编码的问题。

Python 脚本使用 UTF8,尽管我尝试过执行 printf "%s" "${string}" | iconv -t utf-8 | openssl dgst -sha256在 Bash 脚本中,哈希值仍然不同。

在计算 SHA256 总和之前,如何将 bash 字符串/变量转换为 UTF8。

最佳答案

了解如何在 Python 中计算它可能会有所帮助。从我所看到的来看,输出看起来是相同的。

$ python -c "import hashlib; \
print(hashlib.sha256('test'.encode('utf8')).digest().hex())"
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

$ printf "%s" test | openssl dgst -sha256
(stdin)= 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

关于python - Bash 与 Python 中视觉相似字符串的 SHA256 有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730126/

相关文章:

linux - 如何在 bash/linux 中转换/获取 RFC822 日期?

bash - 如何遍历子目录中的文件?

iphone - 如何检查一个字符是否位于某个 unicode 范围之间...?

linux - 算术表达式 : expecting EOF: "008 +1"

java - UTF-8 - 我不明白这个字节序列

perl - Perl 的 IO::File 等价于 open($fh, ">:utf8",$path) 是什么?

python - 如何设置 Selenium 以使用多个 Firefox 配置文件?

python - 我如何让这个容器自行删除?

python - matplotlib 中注释框的坐标

python - .aggregate() 中使用的 Django @property