我正在嘗試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)
}