Elizabeth
Elizabeth

Reputation: 11

How to achieve Base64 URL safe encoding for binary hash string in Powershell?

I am getting the “input” from the server in “Base64-encoded” form as shown in picture.

  1. I need to decode it into its original form of Binary string .i.e original = base64_decode(input) .
  2. concatenate i.e to_be_hash =password (known value) + input.
  3. Hash the concatenated string using SHA-256. i.e binary_hash_str =sha256(to_be_hash).

I need Base64 URL-safe encode the binary hash string above to make it suitable for HTTP requests.

final_hash = base64_url_safe_encode(binary_hash_str)

I am using powershell for this. Can someone guide me how to progress please.

Upvotes: 1

Views: 2796

Answers (2)

Delphi.Boy
Delphi.Boy

Reputation: 1216

The correct way of doing this is shown here: https://www.powershellgallery.com/packages/Posh-ACME/2.0.1/Content/Private%5CConvertTo-Base64Url.ps1

Base64Url-encode is not the same as Base64-encode + Url-encode.

Upvotes: 1

Daniel
Daniel

Reputation: 5114

If I understand correctly you would like to send a base64 string as a argument in a url? You can escape the characters that are not acceptable in a url using [uri]::EscapeDataString()

$text = "some text!!"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)
$base64 = [System.Convert]::ToBase64String($bytes)
$urlSafeString = [uri]::EscapeDataString($base64)

"Base64        : " + $base64
"urlSafeString : " + $urlSafeString

Base64        : cwBvAG0AZQAgAHQAZQB4AHQAIQAhAA==
urlSafeString : cwBvAG0AZQAgAHQAZQB4AHQAIQAhAA%3D%3D

Upvotes: 2

Related Questions