sign a message and verify a signature with node-rsa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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