.net - GitHub sha 不一样

标签 .net git powershell github sha

如何计算与 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 哈希

关于.net - GitHub sha 不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68010201/

相关文章:

c# - 停止调试器后如何启动清理方法?

git - 如何删除gerrit(3.0)中的帐户

powershell - 排队 MSI 安装 - 通过 Powershell

PowerShell 将散列的内容输出到文件

c# - 我是否违反了共同封闭原则(CCP)

.net - NET 文件/MIME 类型检测库

.net - Windsor 拦截器 AOP 和缓存

angular - 在 Azure 中部署私有(private) npm 包

Git:在 merge 提交上保留分支拓扑

c# - Powershell C#异步执行