问题描述
如何计算与 GitHub 上相同的哈希?
我看到了 this 个答案,但结果仍然不同:
$file = "https://raw.githubusercontent.com/itm4n/Perfusion/master/Perfusion.sln"
$out = "D:\bu\2\1\45\Perfusion.sln"
$res = Iwr $file
$Content = "blob 2326\x00"+$res.Content
$Content|out-file $out
Get-filehash -Algorithm SHA1 $out
输出:FCB24D664A2FB8D8F783A8D14C5087A276BC94E9
但是here我看到c226191c92d0a7e43550f40f198ed96df9e65724
解决方法
在可扩展字符串中使用转义序列 `0
来表达文字 NUL:
# download file
$file = "https://raw.githubusercontent.com/itm4n/Perfusion/master/Perfusion.sln"
$res = Invoke-WebRequest $file
# construct git blob
$encoding = [System.Text.Encoding]::UTF8
$content = $encoding.GetBytes($res.Content)
# here's that NUL byte
$blob = $encoding.GetBytes("blob $($content.Length)`0") + $content
# calculate sha1
$sha1 = [System.Security.Cryptography.SHA1Managed]::Create()
$hash = $sha1.ComputeHash($blob)
# print hash string
$hashString = [System.BitConverter]::ToString($hash).Replace('-','').ToLower()
Write-Host $hashString
这应该会产生正确的 blob 哈希