首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Node转换为ASP.NET C# for HMAC_SHA256 Hash

将Node转换为ASP.NET C# for HMAC_SHA256 Hash
EN

Stack Overflow用户
提问于 2020-06-06 18:02:02
回答 1查看 649关注 0票数 0

我正在尝试将这个Node代码转换为ASP.NET C#:

代码语言:javascript
运行
复制
    const crypto = require('crypto');
/**
 * Validates a billing service webhook
 *
 * @param {string} req    Node request object, where 'req.body' is a Node
 *                        Buffer object containing the request body
 * @param {string} secret the secret string saved in the service App
 */
const isValidSignature = (req, secret) => {
  const fsSignature = req.headers['X-FS-Signature'];
  const computedSignature = crypto.createHmac('sha256', secret)
    .update(req.body)
    .digest()
    .toString('base64');
  return fsSignature === computedSignature;
}

以下是我在C#中的尝试

代码语言:javascript
运行
复制
    private bool CheckNotificationValidContextual(string varRequestHashValue, string varMessageBody) // #2047
    {

        // This involves a test of the webhook functionality using ngroks and Postman
        // to send values that were previously generated from a test webhook from billing service
        // to a local development copy of the seller application (running in Visual Studio)
        // where the inputs are:
        // varRequestHashValue = Request.Headers["X-Fs-Signature"];
        // varMessageBody = new System.IO.StreamReader(Request.InputStream).ReadToEnd();

        // get the local copy of the webhook secret key from the local web config file
        var AMPTK_FSP_HMAC_SHA256_Key = ConfigVal.AMPTK_FSP_HMAC_SHA256();

        // convert the local copy of the secret key to a byte array
        byte[] AMPTK_keyBytes = Encoding.UTF8.GetBytes(AMPTK_FSP_HMAC_SHA256_Key);

        // create a hash object with the local copy of the secret key
        var _hashObjectOfLocalKey = new HMACSHA256(AMPTK_keyBytes);

        // convert the input webhook message body to a byte array
        byte[] _messageBodyByteArray = Encoding.UTF8.GetBytes(varMessageBody);

        // create a hash byte array of the message body byte array
        // using the hash object based on the local copy of the webhook secret
        byte[] _computedMessageHashBytes = _hashObjectOfLocalKey.ComputeHash(_messageBodyByteArray);

        // convert the hash byte array of the message body to a string
        string _stringOfComputedMessageHashBytes = Encoding.UTF8.GetString(_computedMessageHashBytes, 0, _computedMessageHashBytes.Length);

            // remove dashes and convert to lowercase
            _stringOfComputedMessageHashBytes= BitConverter.ToString(_computedMessageHashBytes).Replace("-", "").ToLower();


        // compare the string of the computed message body hash
        // to the received webhook secret hash value from Request.Headers["X-Fs-Signature"]
        if (_stringOfComputedMessageHashBytes == varRequestHashValue)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

C#代码编译并运行ok。

我要找的结果是:

prNdADI26M0ov5x6ZlMr2J2zzB8z2TJRBDy+8gjPttk=

我从C#代码"_stringOfComputedMessageHashBytes“中得到了什么

这是:

f37cdae653e167e36c8ed17e44ffa456832dbb7dcec1d00dc1b44a1234965e73

我仔细检查了输入(可能还是错的)。

问:我是否正确地将Node代码翻译成C#,如果没有,我如何改进它?或者还会有什么问题?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-28 20:14:14

我检查了计算哈希的C#代码,一般来说,您使用了正确的库和正确的类HMACSHA256来计算HMAC_SHA256哈希。问题似乎在于您试图将计算出来的哈希值转换为基本64字符串的部分。可以使用Convert.ToBase64String方法将字节数组转换为基本64字符串。

在下面,您可以找到更新的C#代码(我做了一些小小的改进,并更新了代码以找到_stringOfComputedMessageHashBytes)。

我测试了node.jsC#生成的哈希结果,现在它们都生成了相同的哈希结果:

代码语言:javascript
运行
复制
    private bool CheckNotificationValidContextual(string varRequestHashValue, string varMessageBody) // #2047
    {
        // This involves a test of the webhook functionality using ngroks and Postman
        // to send values that were previously generated from a test webhook from billing service
        // to a local development copy of the seller application (running in Visual Studio)
        // where the inputs are:
        // varRequestHashValue = Request.Headers["X-Fs-Signature"];
        // varMessageBody = new System.IO.StreamReader(Request.InputStream).ReadToEnd();

        // get the local copy of the webhook secret key from the local web config file
        var AMPTK_FSP_HMAC_SHA256_Key = ConfigVal.AMPTK_FSP_HMAC_SHA256();

        // convert the local copy of the secret key to a byte array
        var AMPTK_keyBytes = Encoding.UTF8.GetBytes(AMPTK_FSP_HMAC_SHA256_Key);

        // create a hash object with the local copy of the secret key
        using (var _hashObjectOfLocalKey = new HMACSHA256(AMPTK_keyBytes))
        {
            // convert the input webhook message body to a byte array
            var _messageBodyByteArray = Encoding.UTF8.GetBytes(varMessageBody);

            // create a hash byte array of the message body byte array
            // using the hash object based on the local copy of the webhook secret
            var _computedMessageHashBytes = _hashObjectOfLocalKey.ComputeHash(_messageBodyByteArray);

            var _stringOfComputedMessageHashBytes = Convert.ToBase64String(_computedMessageHashBytes);

            // compare the string of the computed message body hash
            // to the received webhook secret hash value from Request.Headers["X-Fs-Signature"]
            return _stringOfComputedMessageHashBytes == varRequestHashValue;
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62235870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档