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

node-crypto-js

v1.0.3

Published

Node (RSA+AES) encryption and decryption companion for hybrid-crypto-js

Downloads

309

Readme

Node Crypto JS

NPM

Introduction

Node Crypto JS is a NodeJS (RSA+AES) encryption and decryption companion for hybrid-crypto-js. Node Crypto JS combines RSA and AES encryption algorithms making it possible to efficiently encrypt and decrypt large messages. This node library is based on hybrid-crypto-js.

Documentation

Getting started

Features

Installation

npm install node-crypto-js

Importing

Node.js

var RSA = require('node-crypto-js').RSA;
var Crypt = require('node-crypto-js').Crypt;

Features

Initialization

// Basic initialization
var crypt = new Crypt();
var rsa = new RSA();

// Increase amount of entropy
var entropy = 'Random string, integer or float';
var crypt = new Crypt({ entropy: entropy });
var rsa = new RSA({ entropy: entropy });

Encryption

Node Crypto JS provides basic encryption function which supports also multiple RSA keys, with or without signature. Encrypted message is outputted as a JSON string.

var message = 'Hello world!';

// Encryption with one public RSA key
var encrypted = crypt.encrypt(publicKey, message);

// Function also supports encryption with multiple RSA public keys
var encrypted = crypt.encrypt([publicKey1, publicKey2, publicKey3], message);

// Encryption with signature
var encrypted = crypt.encrypt(publicKey, message, signature);

Pretty-printed sample output

{
    "v": "node-crypto-js_0.1.2",        // Current package version
    "iv": "CmtyaZTyzoAp1mTNUTztic0v1...", // Initialization vector
    "keys": {                             // Encrypted AES keys by RSA fingerprints
        "85:3d:10:e1:56...": "bHaTF9...",
        "d3:48:6a:e9:13...": "t9eds3..."
    },
    "cipher": "+iwVFsC2dECBQvwcm9DND..."  // Actual encrypted message
    "signature": "sdL93kfdm12feds3C2..."  // Signature (optional)
}

Decryption

Decrypting message with Hybrid Crypto JS is as easy as encrypting. Decryption function can decrypt any message which has been encrypted with keypair's public key. Decrypted message is outputted as a JSON object.

var encrypted = '{"v":"node-crypto-js_0.1.0","iv":"CmtyaZTyzoAp1mTN...';

// Decrypt encryped message with private RSA key
var decrypted = crypt.decrypt(privateKey, encrypted);

// Get decrypted message
var message = decrypted.message;

Sample output

{
    message: "Hello world!",            // Actual decrypted message
    signature: "sdL93kfdm12feds3C2..."  // Signature (optional)
}

Signatures

Hybrid Crypto JS provides simple message signing. When issuer signs a message, message receiver can be sure of the message issuer.

var message = 'Hello world!';

// Create a signature with ISSUER's private RSA key
var signature = crypt.signature(issuerPrivateKey, message);

// Encrypt message with RECEIVERS public RSA key and attach the signature
var encrypted = crypt.encrypt(receiverPublicKey, message, signature);

Verifying

Message receiver needs to have message issuer's public RSA key in order to verify message issuer.

// Encrypted message with signature
var encrypted = '{"v":"hybri... ..."signature":"sdL93kfd...';

// Decrypt message with own (RECEIVER) private key
var decrypted = crypt.decrypt(receiverPrivateKey, encrypted);

// Verify message with ISSUER's public key
var verified = crypt.verify(
  issuerPublicKey,
  decrypted.signature,
  decrypted.message
);

Verification function return true or false depending on whether the verification was successfull.

RSA keypairs

Hybrid Crypto JS RSA key generation function is based in Forge key pair generation function. As a difference Hybrid Crypto JS returns keypair in PEM format.

// Initialize RSA-class
var rsa = new RSA();

// Generate RSA key pair, defaults on 4096 bit key
rsa.generateKeypair(function(keypair) {
  // Callback function receives new keypair as a first argument
  var publicKey = keypair.publicKey;
  var privateKey = keypair.privateKey;
});

// Generate 1024 bit RSA key pair
rsa.generateKeypair(function(keypair) {
  // Callback function receives new 1024 bit keypair as an argument
  var publicKey = keypair.publicKey;
  var privateKey = keypair.privateKey;
}, 1024); // Key size

// RSA can be also initialized with options
var rsa = new RSA({
  keySize: 4096,
  rsaStandard: 'RSA-OAEP' // RSA-OAEP or RSAES-PKCS1-V1_5,
});