【问题标题】:What is the equivalent in nodeJS of the expression below? [closed]下面的表达式在 nodeJS 中的等价物是什么? [关闭]
【发布时间】:2020-06-15 07:05:30
【问题描述】:

谁能帮我在 NodeJs 中完成这项工作

下面是一个示例(C#),简要说明了如何计算签名:我想要它在 Node JS 中

  1. 将积分键的小写字母编码为 UTF-8 格式的字节 var keyToBytes = Encoding.UTF8.GetBytes(integratorKey.ToString().ToLower());
  2. 创建一个使用密钥字节作为加密密钥 var hmacSha256 = new System.Security.Cryptography.HMACSHA256(keyToBytes); 的 HMACSHA256 例程
  3. 通过加密编码端点(仅限 URI 的绝对路径)和时间戳 var signatureBytes = utf8.GetBytes(String.Concat(new Uri(url).AbsolutePath, timeStamp)); 创建签名
  4. 将加密字符串转换为base-64字符串格式var signature = Convert.ToBase64String(hmacSha256.ComputeHash(signatureBytes));

【问题讨论】:

  • 以可执行格式发布参考代码更有意义(关于通过复制/粘贴的再现性)。同时发布您最近的 NodeJS 代码。
  • @Topaco 你是对的。我应该做同样的事情,但不幸的是我在 c# 中也没有任何工作的 sn-p,而且我不理解编码术语,这让我很困惑。我已经尝试了3个多小时,但没有成功。再次感谢

标签: c# node.js encoding utf-8 cryptography


【解决方案1】:

你可以试试这个:

const crypto = require('crypto');
const url = require('url');

const urlExample = 'http://localhost/test/resource';
const integratorKeyString = 'dDASDaGdj5R1kf5151ladAFvv';
const keyToBytes = Buffer.from(integratorKeyString.toLowerCase());
const hmacSha256 = crypto.createHmac('SHA256', keyToBytes);
const signatureBytes = Buffer.from(url.parse(urlExample).pathname + Date.now());
const signature = hmacSha256.update(signatureBytes).digest('base64');

console.log(signature);

【讨论】:

  • 谢谢!有效。非常感谢您的意见。
  • 很高兴我的决定对您有所帮助。关于 cmets 中的感谢 - 您需要阅读此帮助:stackoverflow.com/help/someone-answers 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 2010-11-05
  • 2013-12-16
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多