【问题标题】:How to achieve Base64 URL safe encoding for binary hash string in Powershell?如何在 Powershell 中实现二进制哈希字符串的 Base64 URL 安全编码?
【发布时间】:2021-12-04 23:16:57
【问题描述】:

我正在以“Base64 编码”形式 as shown in picture 从服务器获取“输入”。

  1. 我需要将其解码为二进制字符串的原始形式。即original = base64_decode(input)
  2. 连接即to_be_hash =password (known value) + input
  3. 使用 SHA-256 散列连接的字符串。即binary_hash_str =sha256(to_be_hash)

我需要对上面的二进制哈希字符串进行 Base64 URL 安全编码,以使其适合 HTTP 请求。

final_hash = base64_url_safe_encode(binary_hash_str)

我为此使用了 powershell。有人可以指导我如何进步。

【问题讨论】:

    标签: powershell encoding hash binary base64


    【解决方案1】:

    如果我理解正确,您想在 url 中发送一个 base64 字符串作为参数?您可以使用 [uri]::EscapeDataString() 转义 url 中不可接受的字符

    $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
    
    

    【讨论】:

    • 为了避免转义,大多数 Web 应用程序都希望在 Base64 字符串中使用 - 而不是 =
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2019-08-02
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多