Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sign a message and verify a signature with node-rsa
/// go to https://8gwifi.org/RSAFunctionality?rsasignverifyfunctions=rsasignverifyfunctions&keysize=2048 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');
console.info('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'));
console.info('signature is valid : ' + isValid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.