gpt4 book ai didi

node.js - 如何生成 key 对并使用它来签名和验证 Node.js 中的文本?

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:06 27 4
gpt4 key购买 nike

如何生成私钥和公钥,使用私钥对任意文本的哈希进行签名,然后在其他地方使用公钥来验证签名?

最佳答案

使用URSA (OpenSSL 加密的 Node.js 包装器):

var ursa = require('ursa');

// Generate RSA private key (public key included)
var keyPair = ursa.generatePrivateKey();

// Convert public key to string
var pub = keyPair.toPublicPem('base64');

// Create buffer from text
var data = new Buffer('Hello, world!');

// Create MD5 hash and sign with private key
var sig = keyPair.hashAndSign('md5', data);

// Elsewhere...

// Create public key object from PEM string
pub = ursa.createPublicKey(pub, 'base64');

// Verify signature - should return true
pub.hashAndVerify('md5', data, sig);

关于node.js - 如何生成 key 对并使用它来签名和验证 Node.js 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com