Textual encryption library
npm install iocaneA powerful and easy-to-use text and data encryption library for NodeJS and the web.
  
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:
``javascript`
const iocane = require("iocane"); // node
`javascript`
import * as iocane from "iocane/web" // web
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
Install iocane as a dependency using npm:
`shell`
npm install iocane --save
iocane can be easily used to encrypt text:
`typescript
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:
`typescript
import { createAdapter } from "iocane";
createAdapter()
.decrypt(encryptedString, "myPassword")
.then(decryptedString => {
// ...
});
`
During encryption, you can override a variety of options:
`typescript
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
`typescript
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)._
Iocane can handle buffers the same way it handles strings - just pass them into the same encrypt/decrypt functions:
`typescript
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._
_Available on the Node version only._
Iocane can create encryption and decryption streams, which is very useful for encrypting large amounts of data:
`typescript
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"));
`
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