JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 144
  • Score
    100M100P100Q101573F
  • 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 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

As from node 0.11 the crypto library has publicEncrypt and privateDecrypt 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();

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