npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rsa-keypair

v2.0.2

Published

Generate native openssl RSA keypair

Downloads

196

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.

N-API Example

The module and code also serves as a simple example to create native addon module in C/C++ using N-API.

Upgrade Notes

Version 2.x uses N-API. For older node versions that do not support N-API, use version 1.0.1:

npm install --save [email protected]

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();

ES6:

import rsaKeyPair from "rsa-keypair";
const 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

Develop

git clone https://github.com/sunjith/node-rsa-keypair
cd node-rsa-keypair

# Note: You may use yarn instead of npm in each of the following commands
# Install dependencies and build
npm install

# Build only
npm start

# Run tests
npm run test