Giter Club home page Giter Club logo

rk8-pki's Introduction

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.

rk8-pki's People

Contributors

amadsen avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

rk8-pki's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.