1.2.2 • Published 6 years ago
iv-encrypt v1.2.2
iv-encrypt
Simple encryption functions for node.js projects
Installation
Add it to your node.js project via:
npm install iv-encrypt --save
Usage
First create an encryptor:
// Create iv-encrypt object
const enc = require('iv-encrypt');
To encrypt a string or an object:
// Encrypt a plain string or object to an encrypted & base64 encoded string
var encString = enc.Encrypt({FirstName: 'Tanmay Sathe'});
// Should print some random ascii characters
console.log("Encrypted string: ",encString);
To decrypt a string or an object:
// Decrypt an encrypted string to a plain string or object
var decString = enc.Decrypt(encString);
// Should print `Decrypted object: { FirstName: 'Tanmay Sathe' }`
console.log('Decrypted object: ',decString);
Features
- Encrypt arbitrary objects, not just strings (objects are converted to/from JSON)
- Unique IV per call so no two calls should return the same result for the same input
- Simple algorithm for fastest possible encryption
- No complicated options
- Written to be easy to read
API
The module provides two functions:
encryptor.Encrypt(obj)
- Encrypt the object and return back the encrypted cipher text. The object is first converted to text viaJSON.stringify(...)
. This means you can encrypt arbitrary objects.encryptor.Decrypt(cipherText)
- Decrypts the cipher text and returns the original object. Specifically, it decrypts the cipher text and callsJSON.parse(...)
on the result. If an error occurs during the decryption then null is returned. Note: This function never throws an error for bad input, it just returnsnull
.
Internals
Interally this module uses the pure javascript algorithm for encryption. It then uses the crypto module of node.js to create an iv (intialization vector) buffer. Encryption is done by XORing with a randomly offsetted IV (intialization vector) buffer per call that is returned as part of the result.
Dependencies
No dependencies! It uses crypto module which is built into node.js core.
License
This plugin is released under the MIT license. See the file LICENSE.