在 Powershell 中创建正确的 SHA256 哈希
Creating correct SHA256 hash in Powershell
大家晚上好。我的 sha256 哈希有问题。
我有来自亚马逊页面的示例字符串:
GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
亚马逊显示了这个示例字符串的哈希结果
如下:
**f536975d06c0309214f805bb90ccff089219ecd68b2577efef23edd43b7e1a59**
描述是:这个:哈希规范请求必须表示为一串小写十六进制字符。以下示例显示了使用 SHA-256 对示例规范请求进行哈希处理的结果。
示例哈希规范请求
无论我做什么,我都会收到这个哈希值:
B51325A14138B31939381CB391819CE8A5F09DEEA778721C4360F0DAC1FAB79C
这里有 3 个示例代码:
function hash($request) {
$sha256 = new-object -TypeName System.Security.Cryptography.SHA256Managed
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$hash = [System.BitConverter]::ToString($sha256.ComputeHash($utf8.GetBytes($request)))
return $hash.replace('-','').toLower()
}
function hash2($request){
$mystream = [IO.MemoryStream]::new([byte[]][char[]]$request)
$hash = Get-FileHash -InputStream $mystream -Algorithm SHA256
$hash = $hash.Hash
return $hash.toLower()
}
function hash3($request)
{
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create('sha256')
$hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($request))
$hashString = [System.BitConverter]::ToString($hash)
$hash = $hashString.Replace('-', '')
return $hash.toLower()
}
$string = "GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
hash $string
hash2 $string
hash3 $string
我找到的唯一一个计算与亚马逊相同哈希值的在线计算器是这个:https://xorbin.com/tools/sha256-hash-calculator
这是来自亚马逊的原始内容:https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
有人能帮忙吗?
此致
帕特里克
起初我无法通过 copy-pasting 您的代码重现此行为。然后我将它粘贴到配置为将所有换行符保存为 CRLF
的编辑器中 - 此时我也得到了 B51325A14138B31939381CB391819CE8A5F09DEEA778721C4360F0DAC1FAB79C
.
所以可能的解释是您是在一个编辑器中编写脚本的,该编辑器保存所有带有 Windows-style 换行符的文件。
您可以通过在运行时将结果字符串值中的所有 Windows 样式换行符替换为单个换行符来解决此问题:
hash $string.Replace("`r`n", "`n")
补充Mathias R. Jessen's helpful answer:
封闭脚本文件(.ps1
)的换行格式(Windows CRLF vs. Unix LF)确定脚本中包含的 多行字符串文字 的换行格式(包括 here-string 文字)。
如 Mathias 的回答所示,用 LF 显式替换 CRLF 序列的替代方法是 re-save 您的 .ps1
文件使用 LF 换行符(PowerShell 不会介意)。
- 为了更好的 cross-platform 兼容性,请考虑配置您的编辑器以默认创建 LF-format PowerShell 脚本 。
大家晚上好。我的 sha256 哈希有问题。
我有来自亚马逊页面的示例字符串:
GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
亚马逊显示了这个示例字符串的哈希结果 如下:
**f536975d06c0309214f805bb90ccff089219ecd68b2577efef23edd43b7e1a59**
描述是:这个:哈希规范请求必须表示为一串小写十六进制字符。以下示例显示了使用 SHA-256 对示例规范请求进行哈希处理的结果。
示例哈希规范请求
无论我做什么,我都会收到这个哈希值:
B51325A14138B31939381CB391819CE8A5F09DEEA778721C4360F0DAC1FAB79C
这里有 3 个示例代码:
function hash($request) {
$sha256 = new-object -TypeName System.Security.Cryptography.SHA256Managed
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$hash = [System.BitConverter]::ToString($sha256.ComputeHash($utf8.GetBytes($request)))
return $hash.replace('-','').toLower()
}
function hash2($request){
$mystream = [IO.MemoryStream]::new([byte[]][char[]]$request)
$hash = Get-FileHash -InputStream $mystream -Algorithm SHA256
$hash = $hash.Hash
return $hash.toLower()
}
function hash3($request)
{
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create('sha256')
$hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($request))
$hashString = [System.BitConverter]::ToString($hash)
$hash = $hashString.Replace('-', '')
return $hash.toLower()
}
$string = "GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
hash $string
hash2 $string
hash3 $string
我找到的唯一一个计算与亚马逊相同哈希值的在线计算器是这个:https://xorbin.com/tools/sha256-hash-calculator
这是来自亚马逊的原始内容:https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
有人能帮忙吗?
此致 帕特里克
起初我无法通过 copy-pasting 您的代码重现此行为。然后我将它粘贴到配置为将所有换行符保存为 CRLF
的编辑器中 - 此时我也得到了 B51325A14138B31939381CB391819CE8A5F09DEEA778721C4360F0DAC1FAB79C
.
所以可能的解释是您是在一个编辑器中编写脚本的,该编辑器保存所有带有 Windows-style 换行符的文件。
您可以通过在运行时将结果字符串值中的所有 Windows 样式换行符替换为单个换行符来解决此问题:
hash $string.Replace("`r`n", "`n")
补充Mathias R. Jessen's helpful answer:
封闭脚本文件(
.ps1
)的换行格式(Windows CRLF vs. Unix LF)确定脚本中包含的 多行字符串文字 的换行格式(包括 here-string 文字)。如 Mathias 的回答所示,用 LF 显式替换 CRLF 序列的替代方法是 re-save 您的
.ps1
文件使用 LF 换行符(PowerShell 不会介意)。- 为了更好的 cross-platform 兼容性,请考虑配置您的编辑器以默认创建 LF-format PowerShell 脚本 。