PS SHA512 хеш файла, выводится как строка в кодировке base 64

Мне нужно взять SHA512 из файла, преобразовать в строку в кодировке base 64 и вывести в файл.

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

Я знаю, что эта функция доступна [System.Convert]::ToBase64String

1 ответ

Вы можете сделать это так:

# 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)

Возможно, вы можете объединить некоторые из этих шагов, но это дает вам представление о необходимых шагах

Другие вопросы по тегам