Home > OS >  Hashtable to base64 conversion
Hashtable to base64 conversion

Time:05-09

I'm looking for a way out for converting hash table 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.

CodePudding user response:

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
  • Related