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

rk8-pki

v1.2.0-alpha

Published

Use RSA-KEM to encrypt and decrypt messages.

Downloads

7

Readme

Rk8 PKI

This module wraps up forge.js' RSA KEM logic for easy use. The interface it exposes is, at the highest level, generic enough that the underlying implementation could be changed in the future. However, doing so would mean changing the available configuration settings (and a major version bump).

Installation

npm install --save rk8-pki

Use

This module exports an initialization function to which settings are passed. This function returns an object with 3 methods - keypair, encrypt, and decrypt.

Example:

var initRk8pki = require('../rk8-pki.js');
var rk8pki = initRk8pki({
    pregenerateKeyPairs: 0
});
var assert = require('assert');

var original = 'Original value';

rk8pki.keypair( function (err, keypair) {
    if(err){
        assert.fail(err);
    }
	rk8pki.encrypt(
		original, 
		keypair.publicKey, 
		function (err, encrypted) {
			if(err){
				return assert.fail(err);
			}
			
			rk8pki.decrypt(
				encrypted,
				keypair.privateKey,
				function (err, decrypted) {
					if(err){
						return assert.fail(err);
					}
					
					assert.equal(decrypted.toString(), original, 'Encrypted object decrypts to the same string as the original');
				}
			);
		}
	);
});

Configuration

initRk8pki(settings)

The initialization function is passed a configuration object with these keys:

useNativeCode

Tells us whether we should allow forge.js to use native modules, which can significantly improve performance but may cause portablity issues.

Default: false

cache

An object with two keys - public and private - that determine how many PEM-encoded public or private keys can have their decoded representations cached in an LRU cache so they don't have to be decoded again.

Default: { public: 50, private: 50 }

pregenerateKeyPairs

A number indicating how many keypairs should be generated in the background and cached so that keypair doesn't have to wait for them to be generated and PEM-encoded, which can be a lengthy process.

Default: 10

fork

Generating keypairs, PEM-encoding keys, and decoding PEM-encoded keys can be lengthy processes that block the event loop. By default rk8-pki mitigates this by forking a single child process to handle the actual work. If fork is set to false the work is done on the current process instead.

API

keypair([timeout], callback)

This function generates a 2048 bit PEM-encoded RSA key pair with a public key and a private key.

timeout is the number of milliseconds to wait for the operation to complete.

callback is a function(err, result) to be called when the operation completes or returns an error.

To be documented.

encrypt(message, pemEncodedPublicKey, [timeout], callback)

...uses SHA 256 and AES-GCM and returns an object containing k, iv, m, and t properties, where

  • k - is the RSA-KEM encapsulated secret key
  • iv - is the initialization vector
  • m - is the cipher text (encrypted message)
  • t - is the cipher mode tag

To be documented.

decrypt(encrypted, pemEncodedPrivateKey, [timeout], callback)

encrypted is expected to be an object containing k, iv, m, and t properties, where

  • k - is the RSA-KEM encapsulated secret key
  • iv - is the initialization vector
  • m - is the cipher text (encrypted message)
  • t - is the cipher mode tag

that was created using SHA 256 and AES-GCM.

pemEncodedPrivateKey is a PEM-encode RSA private key

timeout is the number of milliseconds to wait for the operation to complete.

callback is a function(err, result) to be called when the operation completes or returns an error.

To be documented.