Code Monkey home page Code Monkey logo

iocane's Introduction

iocane

A powerful and easy-to-use text and data encryption library for NodeJS and the web.

Buttercup Downloads per month on NPM npm version

About

iocane makes text and data encryption and decryption easy by bundling all the complicated processes into one succinct library. Encrypt and decrypt strings and buffers easily by using iocane's encryption format - string->string / buffer->buffer. Encrypt and decrypt streams in NodeJS.

This library uses "sessions" for encryption and decryption. A session describes one encryption/decryption action, and can also have options be further overridden at the time of execution. Check the examples below for a better idea of how this process works.

iocane works in the browser, too. Both a node version and a web version are available:

const iocane = require("iocane"); // node
import * as iocane from "iocane/web" // web

Features

iocane by default boasts the following features:

  • AES-CBC / AES-GCM encryption:
    • Text
    • Buffers
    • Streams (only in NodeJS)
  • 256bit keys
  • PBKDF2 key derivation (with 250k/custom iterations)
  • 35KB minified web version (10KB gzipped)
  • Overridable encryption/derivation/packing functionality to allow for adaptation to yet-unsupported environments

Installation

Install iocane as a dependency using npm:

npm install iocane --save

Usage

iocane can be easily used to encrypt text:

import { createAdapter } from "iocane";

createAdapter()
    .encrypt("some secret text", "myPassword")
    .then(encryptedString => {
        // do something with the encrypted text
    });

Decryption is even simpler, as instructions on how to decrypt the payload is included in the payload itself:

import { createAdapter } from "iocane";

createAdapter()
    .decrypt(encryptedString, "myPassword")
    .then(decryptedString => {
        // ...
    });

During encryption, you can override a variety of options:

import { EncryptionAlgorithm, createAdapter } from "iocane";

const encrypted = await createAdapter()
    .setAlgorithm(EncryptionAlgorithm.GCM) // use GCM encryption
    .setDerivationRounds(300000)
    .encrypt(target, password);

Each cryptographic function can be overridden by simply replacing it on the adapter

import { createAdapter } from "iocane";

const adapter = createAdapter();
adapter.deriveKey = async (password: string, salt: string) => { /* ... */ };

await adapter.encrypt(/* ... */);

Note that the default encryption mode is EncryptionAlgorithm.CBC (AES-CBC encryption).

Encrypting and decrypting data buffers

Iocane can handle buffers the same way it handles strings - just pass them into the same encrypt/decrypt functions:

import { createAdapter } from "iocane";
import fs from "fs";

createAdapter()
    .setAlgorithm(EncryptionAlgorithm.CBC)
    .encrypt(fs.readFileSync("./test.bin"), "passw0rd")
    .then(data => fs.writeFileSync("./test.bin.enc", data));

The same can be performed on the web, with array buffers in place of standard buffers.

Encrypting and decrypting using streams

Available on the Node version only.

Iocane can create encryption and decryption streams, which is very useful for encrypting large amounts of data:

import { createAdapter } from "iocane";
import fs from "fs";
import zlib from "zlib";

// Encrypt
fs
    .createReadStream("/my-file.dat")
    .pipe(zlib.createGzip())
    .pipe(createAdapter().createEncryptStream("passw0rd"))
    .pipe(fs.createWriteStream("/destination.dat.enc"));

// Decrypt
fs
    .createReadStream("/destination.dat.enc")
    .pipe(createAdapter().createDecryptStream("passw0rd"))
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriteStream("/my-file-restored.dat"));

Web usage

When building a project for the web, make sure to use the web-based version of iocane. Bundling the node version will create super-large bundles and result in slow encryption and decryption. iocane for the web uses UMD so it can be imported or simply loaded straight in the browser as a <script>.

If you load iocane directly in the browser, it will create a global namespace at window.iocane (eg. window.iocane.createAdapter).

Supported environments

iocane supports NodeJS version 10 and above. Node 8 was supported in 3.x and versions prior to 8 were supported in 1.x.

iocane is used in the browser as well - it works everywhere that SubtleCrypto, ArrayBuffer and Promise are available.

Note: iocane is written in TypeScript, though versions before v2 were written in JavaScript.

Buttercup

iocane was originally part of the Buttercup suite. Buttercup is a supported dependent of iocane and efforts are made to align iocane with Buttercup's target platforms and uses.

iocane's People

Contributors

backus avatar kylart avatar perry-mitchell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

iocane's Issues

Add web version

Build a version, with PBKDF2 and encryption patched, for the web.

Why are a random amount of PBKDF2 rounds used?

A random number between derivedKeyIterationsMin and derivedKeyIterationsMax is chosen. Why is this?

iocane/source/derive.js

Lines 15 to 20 in 91f37af

function sanitiseRounds(rounds) {
return rounds || getRandomInRange(
getConfigValue("derivedKeyIterationsMin"),
getConfigValue("derivedKeyIterationsMax")
);
}

Is it so if a user re-uses a password they still have a unique key?

If so, why is this beneficial? If someone successfully brute forced a key they would have generated the source password text in the process. They could regenerate the other keys by running the password through PBKDF2 the required amount of rounds.

Does this lib mach python's Passlib behavior?

Hello, from the past 3 days i can't find a nodejs library that can hash an password just like python passlib, i'm probably missing something but doesn't no why.

hash_python = '$pbkdf2-sha512$25000$0HrPeU8pJUTonbP2HgPgfA$fSsikakW8sa7HRP2V44DLk5bnmUCrd7kW8oVNbC.npVqgYhKrenTQ4rAQK6g4XQPX2K6.iNWOITz2X0aLOSA3A'
password = 'admin'
salt = hash_python.split('$')[3]
rounds = parseInt(hash_python.split('$')[2])
hash_final = hash_python.split('$')[4]
sha = 'sha512'

const passwordHash = require('pbkdf2-password-hash')
passwordHash.hash(password,salt,{iterations: rounds})
  .then((hash) => {
    var string = hash.split('$')[4]
    console.log('HASH-passwordHash ' + string.toString('base64'));
// =>HASH-passwordHash fSsikakW8sa7HRP2V44DLk5bnmUCrd7kW8oVNbC+npVqgYhKrenTQ4rAQK6g4XQPX2K6+iNWOITz2X0aLOSA3A==
  })

Can i achieve the correct hash with this lib?
Thanks if you are reading this.

Keyfile data support

Allow the user to provide the data from a keyfile binary to use for derivation, rather than just the filename.

encryptedContent.split is not a function

Hello,

I get this error:

TypeError: encryptedContent.split is not a function
    at Object.unpackEncryptedContent (/home/svipben/Documents/RNM-client/node_modules/iocane/source/packers.js:15:43)
    at Object.decryptWithPassword (/home/svipben/Documents/RNM-client/node_modules/iocane/source/crypto.js:93:43)
    at Object.<anonymous> (/home/svipben/Documents/RNM-client/test.js:11:8)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7)

JS file:

const crypto = require('iocane').crypto;

const encrypted = async (data, password) => {
  const encryptWithPassword = await crypto.encryptWithPassword(data, password);
  console.log(encryptWithPassword);
};

const enc = encrypted('testtest', 'svipben');

const decrypted = async (data, password) => {
  const decryptWithPassword = await crypto.decryptWithPassword(data, password);
  console.log(decryptWithPassword);
};

decrypted(enc, 'svipben');

Async encryption/decryption

The encryption happens synchronously which blocks the event loop meaning the browser/node process hangs and can't do anything else until encryption has finished.

It would be much better if encryption/decryption happened asynchronously. Any plans to support this? Would you accept a PR?

Decryption with key file fails

Decryption, when using a key file, fails with the following error:

Error: Encrypted content has been tampered with
    at Object.module.exports.decrypt (/Users/pez/Git/iocane/source/crypto.js:65:23)
    at derivation.deriveFromFile.then (/Users/pez/Git/iocane/source/crypto.js:76:50)

How to know the default options and parameters?

Hi, I am using the method encryptWithPassword and I'm wondering what kind of deafaults are there. The salt, the iterations, the iv etc? Also I've merked that this method is slow. Is it encryptWithPassword and decryptWithPassword are using under the skin asynchronous functions?

Argon2

Support password derivation using argon2id. Requires that the rounds parameter of the output string support being encoded as a string to include argon config.

Libraries:

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.