I want to Base64 URL-safe encode my "binary hash string" value that comes as input from my previous code line to make it suitable for HTTP requests, i.e. final_hash = base64_url_safe_encode(binary_hash_str). Where i can use the final hashed password for authentication purpose in powershell. I was not able to find any code in powershell.
Powershell Base64 URL-safe encode the binary hash string above to make it suitable for HTTP requests
Asked
Active
Viewed 698 times
0
-
3"for authentication purpose" - do you intend to pass it as part of a header (eg. for Basic authentication, `Authorization: Basic YWRtaW46...`), or as a query parameter in the URL (eg. `https://domain.tld/some/path?key=YWRtaW46...`)? – Mathias R. Jessen Oct 20 '21 at 12:12
-
Hashed password will be sent here: $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json") $body = "{ username: user, password: hashed_password }" $response = Invoke-RestMethod 'https://192.158.10.21/api/token' -Method 'POST' -Headers $headers -Body $body $response – Elizabeth Oct 20 '21 at 20:42
-
When the server receives the hashed password it will send me a token which i will be passing as a header ("Authorization", "Bearer access token") – Elizabeth Oct 20 '21 at 20:44
1 Answers
2
Converting a byte array to base64 in PowerShell can be done with [Convert]::ToBase64String()
:
$binString = [System.Text.Encoding]::UTF8.GetBytes("Secret string")
$base64String = [Convert]::ToBase64String($binString)
If you want to use the resulting base64 string as part of the request payload (eg. a header or part of the request body), you can go ahead and use it as is:
Invoke-WebRequest $uri -Header @{ Authorization = "Basic ${base64String}"}
If you need to pass it as a query parameter in the url itself, escape it with [uri]::EscapeDataString()
:
$URI = 'https://domain.tld/path?param={0}' -f [uri]::EscapeDataString($base64String)
Invoke-WebRequest $URI

Mathias R. Jessen
- 157,619
- 12
- 148
- 206