GitHub sha is different

Viewed 151

How to calculate the same hash as on GitHub?

I saw this answer, but result is still different:

$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

Output : FCB24D664A2FB8D8F783A8D14C5087A276BC94E9 But here i see c226191c92d0a7e43550f40f198ed96df9e65724

1 Answers

Use the escape sequence `0 in an expandable string to express a literal 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

This should produce the correct blob hash

Related