C#base16编码的SHA256字符串功能未返回预期结果

发布于 2025-01-22 08:08:00 字数 2731 浏览 5 评论 0原文

我有一个函数正在尝试返回一个较低的案例底座16编码的签名主体,以签署AWS API,如Hashed有效载荷部分在这里

给定以下JSON字符串 -

{
  "shipTo": {
    "name": "A3",
    "addressLine1": "SWA Test Account",
    "addressLine2": "SWA Test Account",
    "addressLine3": "SWA Test Account",
    "stateOrRegion": "",
    "postalCode": "DN1 1QZ",
    "city": "Doncaster",
    "countryCode": "GB",
    "email": "[email protected]",
    "phoneNumber": "444-444-4444"
  },
  "shipFrom": {
    "name": "A1",
    "addressLine1": "4 Neal Street",
    "stateOrRegion": "",
    "postalCode": "WC2H 9QL",
    "city": "London",
    "countryCode": "GB",
    "email": "[email protected]",
    "phoneNumber": "444-444-4444"
  },
  "packages": [
    {
      "dimensions": {
        "length": 3.14,
        "width": 3.14,
        "height": 3.14,
        "unit": "INCH"
      },
      "weight": {
        "unit": "KILOGRAM",
        "value": 3.14159
      },
      "items": [
        {
          "quantity": 1,
          "itemIdentifier": "V-02",
          "description": "Sundries",
          "isHazmat": false,
          "weight": {
            "unit": "KILOGRAM",
            "value": 1.14159
          }
        },
        {
          "quantity": 1,
          "itemIdentifier": "V-01",
          "description": "Sundries",
          "isHazmat": false,
          "weight": {
            "unit": "KILOGRAM",
            "value": 1.14159
          }
        }
      ],
      "insuredValue": {
        "unit": "GBP",
        "value": 29.98
      },
      "packageClientReferenceId": "abcd"
    }
  ],
  "channelDetails": {
    "channelType": "EXTERNAL"
  }
}

我期望71D826B7BAB4AF808F7303E81154BCE32FED6F127039393D8B0FA05839A6FB29在成功的Postman中显示的JSON值。

但是,我的价值为16F1382CC1DB6988693B66E49597811E46F4210C284C82CBC37B6A9F58C61BF5,导致随后的签名值,并导致签名值,并导致失败。

这是采用JSON字符串并返回哈希的函数 -

public static string HashRequestBody(string stringToHash)
{
    byte[] hashedBytes = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(stringToHash));
    
    StringBuilder sb = new StringBuilder();
    
    for (int i = 0; i < hashedBytes.Length; i++)
    {
        sb.Append(hashedBytes[i].ToString("x2", CultureInfo.InvariantCulture));
    }

    return sb.ToString();
}

与我的代码中的Hashed有效负载值差异,而不是邮递员的postman呼叫相同的请求主体?

I have a function that is attempting to return a lower case base 16 encoded hash of a request body for signing the AWS API, as detailed in the hashed payload section here.

Given the following JSON string -

{
  "shipTo": {
    "name": "A3",
    "addressLine1": "SWA Test Account",
    "addressLine2": "SWA Test Account",
    "addressLine3": "SWA Test Account",
    "stateOrRegion": "",
    "postalCode": "DN1 1QZ",
    "city": "Doncaster",
    "countryCode": "GB",
    "email": "[email protected]",
    "phoneNumber": "444-444-4444"
  },
  "shipFrom": {
    "name": "A1",
    "addressLine1": "4 Neal Street",
    "stateOrRegion": "",
    "postalCode": "WC2H 9QL",
    "city": "London",
    "countryCode": "GB",
    "email": "[email protected]",
    "phoneNumber": "444-444-4444"
  },
  "packages": [
    {
      "dimensions": {
        "length": 3.14,
        "width": 3.14,
        "height": 3.14,
        "unit": "INCH"
      },
      "weight": {
        "unit": "KILOGRAM",
        "value": 3.14159
      },
      "items": [
        {
          "quantity": 1,
          "itemIdentifier": "V-02",
          "description": "Sundries",
          "isHazmat": false,
          "weight": {
            "unit": "KILOGRAM",
            "value": 1.14159
          }
        },
        {
          "quantity": 1,
          "itemIdentifier": "V-01",
          "description": "Sundries",
          "isHazmat": false,
          "weight": {
            "unit": "KILOGRAM",
            "value": 1.14159
          }
        }
      ],
      "insuredValue": {
        "unit": "GBP",
        "value": 29.98
      },
      "packageClientReferenceId": "abcd"
    }
  ],
  "channelDetails": {
    "channelType": "EXTERNAL"
  }
}

I am expecting a return value of 71d826b7bab4af808f73033e81154bce32fed66f1270393d8b0fa05839a6fb29 as displayed in successful postman calls for the same JSON value.

However, I am getting a value of 16f1382cc1db6988693b66e49597811e46f4210c284c82cbc37b6a9f58c61bf5, which throws off subsequent signing values, and causes the call to fail.

Here is the function that takes the JSON string and returns the hash -

public static string HashRequestBody(string stringToHash)
{
    byte[] hashedBytes = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(stringToHash));
    
    StringBuilder sb = new StringBuilder();
    
    for (int i = 0; i < hashedBytes.Length; i++)
    {
        sb.Append(hashedBytes[i].ToString("x2", CultureInfo.InvariantCulture));
    }

    return sb.ToString();
}

What would be causing the differences in hashed payload values in my code as opposed to the Postman call for the same request body?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2025-01-29 08:08:00

事实证明,由于我的JSON对象中包含的线路断裂,哈希是不同的。

您可以阅读更多在这里Hash Amazon正在计算,仅将\ n用作线路。

我通过将对象序列化为字符串并用预期\ n值替换\ r \ n来解决此问题

: \ n“);

It turns out that the hash was different due to the line breaks included in my JSON object.

You can read more here, but the short of it is Microsoft uses \r\n for line breaks by default, and the hash Amazon is calculating is using only \n as a line break.

I resolved this by serializing the object to a string and replacing \r\n with the expected \n values, like so:

body = Regex.Replace(body, @"\r\n?|\n", "\n");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文