I’m looking for a way out for converting hashtable to base64 in powershell. Not sure that’s possible or not. Can anyone help me on this?
Strings can be converted to base64 via byte. I’m seeing only string conversion examples.
Please help me to figure this out to proceed further.
Thanks.
>Solution :
Convert the hash to JSON. Convert the JSON to Base64.
$hash = @{key = "value"}
# encode
$json = $hash | ConvertTo-JSON -Depth 100 -Compress
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json)
$b64 = [System.Convert]::ToBase64String($bytes) # => 'eyJrZXkiOiJ2YWx1ZSJ9'
# decode
$bytes = [System.Convert]::FromBase64String($b64)
$json = [System.Text.Encoding]::UTF8.GetString($bytes)
$hash = $json | ConvertFrom-Json