Coder Social home page Coder Social logo

pwfoo / hybrid-crypto-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juhoen/hybrid-crypto-js

0.0 3.0 0.0 382 KB

RSA+AES hybrid encryption implementation for JavaScript. Including React Native key management.

Home Page: https://juhoen.github.io/hybrid-crypto-js/

License: MIT License

JavaScript 100.00%

hybrid-crypto-js's Introduction

Hybrid Crypto JS

NPM

Introduction

Hybrid Crypto JS is a hybrid (RSA+AES) encryption and decryption toolkit for JavaScript, including automatic and persistent key management on React Native. Hybrid Crypto JS combines RSA and AES encryption algorithms making it possible to efficiently encrypt and decrypt large messages. This cross-platform library is based on Forge.

Documentation

Getting started

Features

Installation

npm install hybrid-crypto-js

Importing

Node.js

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

React Native

import {Crypt, keyManager, RSA} from 'hybrid-crypto-js';

Web

Download minified hybrid-crypto.min.js file here.

<script type="text/javascript" src="hybrid-crypto.min.js"></script>

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

Hybrid 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": "hybrid-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":"hybrid-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);

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, 
});

React Native key management

Key manager works when using React Native. It automatically generates, saves and fetches device specific keypair from the device's storage.

import {keyManager} from 'hybrid-crypto-js';

// Get device specific RSA key pair
keyManager.getKeys(function(keypair) {

    // Callback function receives new keypair as a first argument
    var publicKey = keypair.publicKey;
    var privateKey = keypair.privateKey;
});

hybrid-crypto-js's People

Contributors

juhoen avatar

Watchers

 avatar  avatar  avatar

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.