Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created November 16, 2021 10:58
Show Gist options
  • Save nkhil/68b0aa8477624382540e6c3ea7650451 to your computer and use it in GitHub Desktop.
Save nkhil/68b0aa8477624382540e6c3ea7650451 to your computer and use it in GitHub Desktop.

Here's how to create and export an RSA public/private key pair from Node.js

const crypto = require('crypto')
const fs = require('fs')

const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
})

fs.writeFileSync('private_test_n1.pem', privateKey.export({
  type: 'pkcs1',
  format: 'pem',
}))

fs.writeFileSync('public_test_n1.pem', publicKey.export({
  type: 'pkcs1',
  format: 'pem',
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment