我需要获取文件的SHA512,转换为以base 64编码的字符串,然后输出到文件。
PS > Get-FileHash <path to file> -Algorithm SHA512
我知道此功能可用[System.Convert]::ToBase64String
答案 0 :(得分:2)
您可以这样做:
# Get the hash of the file
$hash = Get-FileHash <path to input file> -Algorithm SHA512
# Convert hash to byte representation
$hashBytes = [System.Text.Encoding]::Unicode.GetBytes($hash.Hash)
# Convert bytes to base64, then output to file
[System.Convert]::ToBase64String($hashBytes) | Out-File <path to hash output file>
执行以下操作再次恢复哈希:
# Read encoded string from text file
$encodedHash = Get-Content <path to hash output file>
# Convert to from base64 to byte representation
$hashBytes = [System.Convert]::FromBase64String($encodedHash)
# Convert bytes to hash string
[System.Text.Encoding]::Unicode.GetString($hashBytes)
您可能可以合并其中一些步骤,但这可以使您了解所需步骤