使用流的json创建hmachash的hmachash,请求有所不同

发布于 2025-01-30 10:20:58 字数 1692 浏览 2 评论 0原文

我想再生一个令牌以使用hmachash验证请求 资源文档在JavaScript中,我必须在C#中实现它。 在这里,我在他们在生成令牌的文档中发现的内容。

routes.post("/webhook", (request) => {
  const body          = request.rawBody;
  const signatureTime = "2022-05-17T09:47:51.935Z";
  const signingKey = "localyStoredKey";

  const hmac = createHmac("sha256", signingKey);
  hmac.update(`${signatureTime}${body}`);

  const token = hmac.digest("hex");
}

C#中所做的工作

public async Task<string> GetWebhookAuthenticationTokenAsync(Stream requestBody) // requestBody is from HttpContext.Request.Body
{
    var signingKey = "localyStoredKey"; // should be from db
    var signatureTime = "2022-05-17T09:47:51.935Z"; // a timestamp

    var reader = new StreamReader(requestBody, Encoding.UTF8);
    var body = await reader.ReadToEndAsync();
    var byteArray1 = Encoding.UTF8.GetBytes($"{signatureTime}");
    var byteArray2 = Encoding.UTF8.GetBytes(body);

    var concatenatedByteArray = byteArray1.Concat(byteArray2).ToArray();
    var stringInMemoryStream = new MemoryStream(concatenatedByteArray);
    
    using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(signingKey));
    var hashBytes = hmac.ComputeHash(stringInMemoryStream);

    var token = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToLower();
    return token;
}

这是我在没有newline字符(\ n)的情况下发送请求主体(json)时在 。 当我向请求发送格式化的JSON主体时,令牌与生成的令牌不同。我想念什么? 只需JSON是{“ Pro”:“ Duct”}令牌是匹配的,但是当Json {\ n“ Pro”:“ Duct” \ n}与众不同。

PS:我发现问题是由于JSON主体中的托架返回\ r。有任何帮助可以独立于平台处理它吗?

I want to regenerate a token to verify the request using hmacHash
The resource documentation is in javascript and I have to implement it in C#.
here what I found in their documentation on generating the token.

routes.post("/webhook", (request) => {
  const body          = request.rawBody;
  const signatureTime = "2022-05-17T09:47:51.935Z";
  const signingKey = "localyStoredKey";

  const hmac = createHmac("sha256", signingKey);
  hmac.update(`${signatureTime}${body}`);

  const token = hmac.digest("hex");
}

here is what I have done in C#

public async Task<string> GetWebhookAuthenticationTokenAsync(Stream requestBody) // requestBody is from HttpContext.Request.Body
{
    var signingKey = "localyStoredKey"; // should be from db
    var signatureTime = "2022-05-17T09:47:51.935Z"; // a timestamp

    var reader = new StreamReader(requestBody, Encoding.UTF8);
    var body = await reader.ReadToEndAsync();
    var byteArray1 = Encoding.UTF8.GetBytes(
quot;{signatureTime}");
    var byteArray2 = Encoding.UTF8.GetBytes(body);

    var concatenatedByteArray = byteArray1.Concat(byteArray2).ToArray();
    var stringInMemoryStream = new MemoryStream(concatenatedByteArray);
    
    using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(signingKey));
    var hashBytes = hmac.ComputeHash(stringInMemoryStream);

    var token = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToLower();
    return token;
}

This works fine when I send the request body(json) without newline characters(\n).
when I send the formatted json body with the request, the token is different from javascript generated token. What am I missing?
simply if json is {"pro": "duct"} the tokens are matched but when json {\n "pro": "duct"\n} its different.

PS: I found the issue is due to Carriage Return \r in json body. Any help to handle this independently from the platform?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文