2

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

Danny
  • 410
  • 2
  • 8
  • Where is `$a` defined? Where is `$res` used? Should `$a` be `$res`? If so, `$res.content.length` is 2324, not 2326, which may throw off your stuff. – TheMadTechnician Jun 16 '21 at 21:45
  • 3
    Per https://stackoverflow.com/a/55679654/3156906, ```\x00``` represents a single null (zero) byte, but that string in PowerShell is a literal string, not an escape sequence. You need to replace it with a string that represents a null byte. – mclayton Jun 16 '21 at 21:52

1 Answers1

4

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206