PS SHA512文件的哈希值,输出为基数为64的编码字符串

问题描述 投票:1回答:1

我需要取一个文件的SHA512,转换为base 64编码的字符串,然后输出到文件。

PS > Get-FileHash <path to file> -Algorithm SHA512

我知道这个功能可用[System.Convert]::ToBase64String

powershell base64 sha512 get-filehash
1个回答
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)

您可以将这些步骤中的一些组合起来,但这会为您提供所需步骤的图片

© www.soinside.com 2019 - 2024. All rights reserved.