简体   繁体   English

如何在node.js中创建用于签名数据的公钥和私钥?

[英]How do I create a public and private key in node.js for signing data?

Using only node.js I want to achieve the following : 我只想使用node.js来实现以下目标:

  • Create a strong random private/public key that is human readable. 创建易于理解的强随机私人/公共密钥。

  • Take a string data and the private key to create a signature in base64 获取字符串数据和私钥以在base64中创建签名

  • Take a string data, the public key and the signature and confirm that the signature was created by the owner of the private key. 获取字符串数据,公钥和签名,并确认签名是由私钥的所有者创建的。

So far I have got the following : 到目前为止,我有以下内容:

var crypto = require('crypto');
var message = "This is a string I want to ensure is not tampered with.";
var sign = crypto.createSign("RSA-SHA256");
sign.update(message);
var signature = sign.sign("thisiswhereiassumetheprivatekeywillgo", "base64");

console.log(message);
console.log(signature);

but the console output only shows the message, the signature returns empty. 但是控制台输出仅显示该消息,签名返回空。

Any Help would be much appreciated. 任何帮助将非常感激。

All modern crypto works with binary data, usually represented by bytes. 所有现代加密技术都使用二进制数据,通常以字节表示。 Many scripting languages perform some magic conversion between bytes and characters, but the issue is that byte arrays are usually not just containing printable characters. 许多脚本语言在字节和字符之间执行魔术转换,但是问题在于字节数组通常不仅仅包含可打印字符。

To convert bytes to a human readable form, you need to apply some form of . 要将字节转换为人类可读的形式,您需要应用某种形式。 For debugging purposes it's best to convert to hexadecimals (base 16 encoding). 出于调试目的,最好转换为十六进制(以16为基数的编码)。 For efficiency reasons, you could also use base 64 encoding. 出于效率原因,您也可以使用base 64编码。 Both encodings are specified by RFC4648 . 两种编码均由RFC4648指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM