我正在尝试base64编码从REST平台读取的文件列表上的sha1哈希值。读回的文件工作正常,事实上整个脚本工作正常,但问题是,我只能解决如何Base64编码的SHA1哈希的字符串表示,但我需要B64编码实际的哈希,而不是散列的字符串。在Powershell中的Base64编码
下面是我使用的一些代码,但我不想B64编码散列的字符串,但散列本身。
作为一个例子,我对一个文件的代码输出如下,第一个是由另一个系统计算的散列,B64编码原始散列,第二个散列是由我们的代码创建的散列,B64编码哈希的字符串值完全不同。
任何帮助,将不胜感激。
源文件: /REST/CCLAIMS/053/17667053 AERTRMT1xZNrW9TTl6k6Orryiwc12gtJQfJSnlOeWGI =
目标文件哈希 /REST/CCLAIMS/053/17667053 M0U1NDY0NDk1NEJDNjVBRTNEMEU3M0JBNTkyNzk4QzMwQ0M3MEU2NA ==
Function Get-StringHash([String] $String,$HashName = "MD5") {
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash(
[System.Text.Encoding]::UTF8.GetBytes($String)
)|%{
[Void]$StringBuilder.Append($_.ToString("x2"))
}
$StringBuilder.ToString()
}
$hash = "SHA1"
$filehash = Get-FileHash -Path C:\Temp\PS\output.file -Algorithm $hash
$hashvalue = [system.text.encoding]::UTF8.GetBytes($filehash.Hash)
add-content $outputfile ($line + "," + $filehash.Hash + "," + [system.convert]::ToBase64String($hashvalue))
} Catch {
$errormessage = $_.Exception.Message
add-content $outputerrorfile ($line + "," + "Error "+$errormessage)
}