使用 node.js 加密的数字签名

Tri*_*ara 3 encryption digital-signature node.js

有人可以提供有关如何使用 node.js 实现数字签名的基本演练吗?我已经谷歌搜索,但还没有得到它。说,我有一个 API,我想签名并验证对它的每个 http 请求和它的响应。这就是我目前的想象,请纠正我的错误:

1)我需要为客户端生成一个足够随机的密钥,用于签署请求;

2)结果签名(通过标头提供)是用字符串的密钥散列加密的,它必须包括:

  • 请求正文的哈希值(以防止篡改);
  • 时间戳;
  • 随机数;
  • 网址;
  • HTTP 动词

然后,服务器将能够检查消息的真实性。

问题:

1)nonce(一些随机值)和时间戳(我读过一篇建议同时使用两者的帖子)在这种情况下有什么区别?

2) 我是否需要在该字符串中包含 eTag 标头?

3) 还有什么,上面没有列出,应该包括在内?

4) 我应该在 API 服务器上保留与客户端相同的密钥并使用它解密和检查请求,还是应该将存储在 API 服务器上的密钥和存储在服务器上的密钥与 API 通信私钥-公钥对?如果它们确实需要成为私钥 - 公钥密钥对,我如何在节点中使用公钥(反之亦然)解密用私钥加密的内容?

请纠正我描述中的错误并添加我遗漏的内容。谢谢你。

小智 7

下面的代码示例使用crypto库(现在内置于 nodejs 中)为文档生成数字签名。

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

// See keys/README.md on how to generate this key
const private_key = fs.readFileSync('keys/privateKey.pem', 'utf-8');

// File/Document to be signed
const doc = fs.readFileSync('sample-doc.txt');

// Signing
const signer = crypto.createSign('RSA-SHA256');
signer.write(doc);
signer.end();

// Returns the signature in output_format which can be 'binary', 'hex' or 'base64'
const signature = signer.sign(private_key, 'base64')

console.log('Digital Signature: ', signature);
Run Code Online (Sandbox Code Playgroud)

这是来自 github 的完整代码示例:digital-signature-for-document-signing