Skip to content

Instantly share code, notes, and snippets.

@alexrqs
Created May 24, 2023 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexrqs/ab7d82cd1fa9d31f6dc915893628c0b0 to your computer and use it in GitHub Desktop.
Save alexrqs/ab7d82cd1fa9d31f6dc915893628c0b0 to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
}
});
const data = { foo: "bar" };
const sign = crypto.createSign('sha256');
sign.update(JSON.stringify(data));
sign.end();
const signature = sign.sign({
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
}, 'base64');
const isVerified = crypto.verify(
'sha256',
Buffer.from(JSON.stringify(data)),
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
},
Buffer.from(signature, 'base64')
);
console.log(isVerified);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment