【问题标题】:How to apply a "Hex Digest" to a HMAC SHA256 hashed value in an Azure API Management policy?如何将“十六进制摘要”应用于 Azure API 管理策略中的 HMAC SHA256 哈希值?
【发布时间】:2022-12-09 17:46:38
【问题描述】:

我正在尝试按照 here 的说明验证来自 Slack 的 API 请求。

概括起来步骤是:

  1. 将请求时间戳与版本和请求正文连接起来。
  2. 使用 HMAC SHA256 对上述字符串进行哈希处理,然后使用“十六进制摘要”对其进行哈希处理。
  3. 将上面的签名与 Slack 发送的签名进行比较。

    我在这个post 中应用了建议,即:

    string hexaHash = "";
    foreach (byte b in my_signature)
    {
        hexaHash += String.Format("{0:x2}", b);
    }
    

    我期待这样的价值:

    'a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503'

    当我不应用上面的代码时,我得到的值是这样的:

    'KvFZL2TojhYJj6ahS0Z7etDwSn4='

    应用该代码时对此进行了哪些更改:

    '76303d6f4f31494741457277466e4b32344c6f655172713281ef935fe1fa3d'

    我完整的 Azure API 管理策略代码如下:

    <inbound>
      //Setting variables from the headers passed by Slack
      <set-variable name="timestamp" value="@(context.Request.Headers.GetValueOrDefault("X-Slack-Request-Timestamp"))" />
      <set-variable name="slack_signature" value="@(context.Request.Headers.GetValueOrDefault("X-Slack-Signature"))" />
    
      //Set body received from slack as variable
      <set-variable name="slack_body" value="@(context.Request.Body.As
        <String>(preserveContent: true))" />
      <set-variable name="slack_signing_secret" value="{{Slack-Signing-Secret}}" />
    
      //Create concatenation string as per slack documentation
      <set-variable name="sig_basestring" value="@{
                        string body = (string)context.Variables.GetValueOrDefault("slack_body");
                        string timestamp = (string)context.Variables.GetValueOrDefault("timestamp");
                        string sig_basestring = "v0:" + timestamp + ":" + body;
                        return sig_basestring;
        }" />
                        
        //Apply HMACSHA256 to concatenated string using slack signing secret as key                
       <set-variable name="my_signature" value="@{
                    //Hash-based Message Authentication Code (HMAC) using SHA256 hash
                    System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes("{{Slack-Signing-Secret}}"));
                    return Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes((string)context.Variables["sig_basestring"])));
         }" />
        
        
       //I'm using this method to send the data back to slack to validate both signatures
       <return-response response-variable-name="existing response variable">
          <set-status code="200" reason="OK" />
          <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
          </set-header>
          <set-body>@{
                string my_signature = "v0=" + (string)context.Variables["my_signature"];
                string slack_signature = (string)context.Variables["slack_signature"];
                string hexaHash = "";
                //This code is applying the "hex digest" method I found
                foreach (byte b in my_signature)
                {
                    hexaHash += String.Format("{0:x2}", b);
                }
                my_signature = "v0=" + hexaHash;
                return my_signature + " " + slack_signature;
           }</set-body>
        </return-response>
    </inbound>
    

    有什么我可以应用于当前散列值以获得与 Slack 文档中建议的“hexdigest()”方法类似的结果吗?

【问题讨论】:

    标签: c# slack-api azure-api-management hmacsha256


    【解决方案1】:

    只是为了好玩,我问了这个名为 ChatGPT 的新人工智能这个问题,这是它的答案(我不是 Slack 的专家,也不是 Azure 的专家):

    您编写的代码看起来是正确的,但您在应用十六进制格式后使用的是 my_signature 变量。这就是为什么您看到的值与预期不同的原因。

    要解决此问题,您需要将格式化后的值存储在一个新变量中,并在 set-body 策略中使用该变量。

    这是您如何做到这一点的示例:

    <set-variable name="hex_signature" value="@{
        string my_signature = (string)context.Variables["my_signature"];
        string hexaHash = "";
        foreach (byte b in my_signature)
        {
            hexaHash += String.Format("{0:x2}", b);
        }
        return hexaHash;
    }" />
    
    <!-- Other code goes here -->
    
    <set-body>@{
        string my_signature = "v0=" + (string)context.Variables["hex_signature"];
        string slack_signature = (string)context.Variables["slack_signature"];
        return my_signature + " " + slack_signature;
    }</set-body>
    

    在上面的代码中,我们正在创建一个新变量 hex_signature,其中包含 my_signature 变量的十六进制格式版本。然后我们在 set-body 策略中使用这个 hex_signature 变量来返回正确的值。

    不确定现在应该如何评分。我所做的只是格式化另一个聊天的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多