为什么 sha256 哈希键与命令提示符和 Powershell 不同

Why is the sha256 hash key different from Command Prompt and Powershell

所以我目前正在做一个项目,我遇到了完整性哈希。所以我被教导使用 openssl sha256 来散列一个 css 文件来练习。我在终端、命令提示符和 Powershell 中执行了此操作。

终端给出了与命令提示符相同的结果,但 powershell 给出了全新的结果。

命令提示符 3BmtwdrKmE6lXPHGqB1Z1jEERC8phQpUwTHMblpJ0Gw=

航站楼 3BmtwdrKmE6lXPHGqB1Z1jEERC8phQpUwTHMblpJ0Gw=

PowerShell Pxk/Pz8/P04/XD8/Px1ZPzEERC8pPw0KVD8xP25aST9sDQo=

tl;博士

基本上我只是想知道终端和命令提示符之间对 powershell 的不同输出

额外:

我对网络安全非常感兴趣,我想学习更多,提高技能,做独角兽所需的一切。随意给我 critic/advice <3.

您正在处理命令提示符、PowerShell 和 OpenSSL 对来自这些 shell 的 运行 的解释之间的输出编码差异。

要从命令提示符查看您的活动代码页,运行 chcp.com。要在 PowerShell 中查看您的活动输出编码,请检查 $OutputEncoding 变量的状态。 注意:您将看到代码页差异。

您可能会尝试将两者设置为相同的输出编码类型,但 OpenSSL 很可能仍会报告差异。

例如,您可以查看 PowerShell 中所有输出编码类型的 OpenSSL 输出:

[System.Text.Encoding]::GetEncodings() | % { "`n`nCodePage $($_.CodePage):"; $OutputEncoding = [System.Text.Encoding]::GetEncoding($_.CodePage); openssl dgst -sha256 -binary .\index-styles.css | openssl base64 -A }

注意:与 OpenSSL 的命令提示符输出相比,我怀疑是否列出了类似的散列。

无论如何,为了避免这个问题,我建议使用 OpenSSL 的内置 -out file 参数,然后调用 OpenSSL 两次,而不是依赖管道 (|):

openssl dgst -sha256 -binary -out .\index-styles.out .\index-styles.css
openssl base64 -A -in .\index-styles.out

当使用 -out file 然后 -in file

时,您应该(理论上)在命令提示符和 PowerShell 中从 OpenSSL 获得一致的结果

希望对您有所帮助。