JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q97101F
  • License MIT

Generate native openssl RSA keypair

Package Exports

  • rsa-keypair

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rsa-keypair) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-rsa-keypair

NPM version MIT License Build Status Dependency Status

Generates a RSA keypair using native OpenSSL library.

This is a fork of rsa-keygen with support for encrypting the generated private key with a given pass phrase. The dependencies have also been updated as per the pull request Update deps by omsmith and calvinmetcalf which was not merged in to the original rsa-keygen.

This code is loosely based on ursa RSA generation code.

Thanks to all the developers who have contributed to the above projects.

History

The node crypto library has encrypt and decrypt functions, we don't need to rely on any external libraries for public-key cryptography.

Usage

Install the library using npm:

npm install --save rsa-keypair

Or install using yarn:

yarn add rsa-keypair

Use in your code:

var rsaKeyPair = require("rsa-keypair");
var keys = rsaKeyPair.generate();

API

/**
 * Generate RSA key pair.
 * @param {Number} modulusBits - The RSA key size. Minimum: 512, default: 2048.
 * @param {Number} exponent - The public exponent. Should be relatively prime to p-1 for all primes p which divide the modulus. Default: 65537.
 * @param {String} passPhrase - The pass phrase to encrypt the RSA private key. If not specified the private key shall be unencrypted. Even passing an empty string will cause the private key to be encrypted. Default: no pass phrase.
 * @return {Object} The object containing the private key in property 'privateKey' and the public key in property 'publicKey'. Note: if 'passPhrase' was passed to encrypt the private key, it is not retuned in the result object.
 */
function generate(modulusBits, exponent, passPhrase);

Examples

var crypto = require("crypto");
var rsaKeyPair = require("rsa-keypair");

var keys = rsaKeyPair.generate();

var result = crypto.publicEncrypt(
 {
  key: keys.publicKey
 },
 new Buffer("Hello world!")
);
// <Crypted Buffer>

var plaintext = crypto.privateDecrypt(
 {
  key: keys.privateKey
 },
 result
);
// Hello world!
var crypto = require("crypto");
var rsaKeyPair = require("rsa-keypair");

var keys = rsaKeyPair.generate(4096, 65537, "top secret");
// Generates a 4096-bit RSA key pair with "top secret" as the pass phrase to encrypt the private key

var result = crypto.privateEncrypt(
 {
  key: keys.privateKey,
  passphrase: "top secret",
  padding: crypto.constants.RSA_PKCS1_PADDING
 },
 new Buffer("Hello world!")
);
// <Crypted Buffer>

var plaintext = crypto.publicDecrypt(
 {
  key: keys.publicKey,
  padding: crypto.constants.RSA_PKCS1_PADDING
 },
 result
);
// Hello world!
var rsaKeyPair = require("rsa-keypair");

var keys = rsaKeyPair.generate(4096, 65537, "top secret");
// Generates a 4096-bit RSA key pair with "top secret" as the pass phrase to encrypt the private key

var publicKeyStr = keys.publicKey.toString();
// The public key string in PEM format which may be written to a file

var privateKeyStr = keys.privateKey.toString();
// The encrypted private key in PEM format which may be written to a file