enigma
A fast, native, environment-agnostic, cryptographic engine for the web
import Enigma from '@cubbit/enigma';
const aes = new Enigma.AES();
const my_secret = 'My secret';
const cipher = aes.encrypt(my_secret);
console.log(cipher);
Enigma is a crypto library available both for Node.js platform and for the Web. It relies on OpenSSL to provide the most common cryptographical utilities. In a web environment, Enigma leverages on a WebAssembly-compiled version of OpenSSL to boost performances.
Installation
Enigma is a npm module available through the npm registry.
Installation is done both in Node.js and in a web environment using the npm install
command:
npm install @cubbit/enigma
If you want to work from source, just clone the repo and run the install script as:
git clone https://github.com/cubbit/enigma.git
cd enigma
npm install
Node.js
Before installing, download and install Node.js. Node.js version 8.0 or higher is required (Node.js 11 has not be tested yet).
Enigma is supported on the following platforms.
| x86 | x64 | arm32 | arm64 |
---|
Linux | ︎︎︎ ✔︎ | ✔︎ | ✔︎ | ✔︎ |
macOS | - | ✔︎ | - | - |
Windows | ✔︎ | ✔︎ | - | - |
After installing just import @cubbit/enigma
in your code and you are ready to go.
Web
Install the library by following the Installation section. Then, just import @cubbit/enigma
in your source and use it as you would do on Node.js.
Important: Enigma needs a Buffer polyfill in order to work correctly on the web. The default one provided by webpack is ok. Otherwise you'll need to provide one by yourself.
Features
Enigma includes the following cryptographical utilities:
- Hashing algorithms (SHA256)
- Simmetric encryption algporithms (AES256)
- Asymmetric encryption algorithms (RSA, ECC)
- Misc utilities (Random, Key derivation algorithms)
Please refer to the API section to discover more about how to use each of them
Examples
Hashing
import Enigma from '@cubbit/enigma';
const message = 'Hello world';
const hash = Enigma.Hash.digest(message);
console.log(hash);
Encrypt with AES
import Enigma from '@cubbit/enigma';
const aes = new Enigma.AES();
const my_secret = 'My secret';
const cipher = aes.encrypt(my_secret);
console.log(cipher);
Encrypt a file using AES stream
When encrypting a big file you may encounter browser limitations or memory issues. The AES stream class is design to overcome these problems.
import {createReadStream} from 'fs';
import Enigma from '@cubbit/enigma';
const file_stream = fs.createReadStream('my_secret_image.png');
const aes = new Enigma.AES();
const iv = Enigma.Random.bytes(16);
const aes_stream = aes.encrypt_stream(iv);
aes_stream.once('finish', () => console.log('File encrypted'));
file_stream.pipe(aes_stream);
import Enigma from '@cubbit/enigma';
import WebFileStream from '@cubbit/web-file-stream';
const file = new File();
const file_stream = WebFileStream.create_read_stream(file);
const aes = new Enigma.AES();
const iv = Enigma.Random.bytes(16);
const aes_stream = aes.encrypt_stream(iv);
aes_stream.once('finish', () => console.log('File encrypted'));
file_stream.pipe(aes_stream);
Decrypt with AES
import Enigma from '@cubbit/enigma';
const existing_key =
const aes = new Enigma.AES({key: existing_key});
const message = aes.decrypt(my_secret).toString();
console.log(message);
Generate a RSA keypair
import Enigma from '@cubbit/enigma';
const keypair = Enigma.RSA.create_keypair();
Encrypt and decrypt with RSA
import Enigma from '@cubbit/enigma';
const message = 'My secret';
const rsa = new Enigma.RSA();
const encrypted = Enigma.RSA.encrypt(message, rsa.keypair.public_key);
console.log(encrypted);
const decrypted = rsa.decrypt(encrypted).toString();
console.log(decrypted);
Generate a ECC keypair
import Enigma from '@cubbit/enigma';
const keypair = Enigma.ED25519.create_keypair();
Sign and verify message with ECC
import Enigma from '@cubbit/enigma';
const message = 'To be signed';
const ecc = new Enigma.ED25519();
const signature = ecc.sign(message);
console.log(Enigma.ED25519.verify(message, ecc.keypair.public_key, signature))
Perform a key derivation with pbkdf2
import Enigma from '@cubbit/enigma';
const message = 'Original message';
const salted_key = await Enigma.KeyDerivation.pbkdf2(message);
How to rebuild the bindings
To build the project's bindings just run the following command after cloning the repository:
npm run build
How to run tests
To run the test suite, first install the dependencies, then run npm test
:
npm install
npm test
How to contribute
Feel free to open issue or a pull request to report bugs and suggest new features. Please refer to our Contributions guidelines for more details about the contribution process.
License
MIT