Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
sign a message and verify a signature with node-rsa
/// go to to generate a pair of key for testing
const rsa = require('node-rsa');
const privateKey = '-----BEGIN RSA PRIVATE KEY-----\n' + ... + '-----END RSA PRIVATE KEY-----';
const publicKey = '-----BEGIN PUBLIC KEY-----\n' + ... + '-----END PUBLIC KEY-----';
const message = "hello.bonjour.여보세요";
const messageBuffer = Buffer.from(message, 'utf8');
// sign message with the private key
const signKey = new rsa(privateKey);
const signature = signKey.sign(messageBuffer, 'base64');'generated signature : ' + signature);
// ...
// verify signature with the public key
const verifyKey = new rsa();
verifyKey.importKey(publicKey, 'pkcs8-public');
const isValid = verifyKey.verify(messageBuffer, Buffer.from(signature, 'base64'));'signature is valid : ' + isValid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment