crypton v1.0.8
Crypton 1.x
Node module that provides cryptographic functionalities using crypto for ciphering and bcrypt for encryption.
Installation
In your project root run from command line:
$ npm install -save cryptonExample
Let's start! Include in your node application crypton module:
//require object
var Crypton = require('crypton').Crypton;
//or require factory
var factory = require('crypton');
//create options
var options = {
crypto: {
secretKey: 'o!rDE(Qbrq7u4OV',
algorithm: 'AES-256-CBC',
inputEncoding: 'utf8',
outputEncoding: 'base64'
},
bcrypt: {
saltRounds: 5
}
};
//create an instance
var cryptoManager1 = new Crypton(options);
//or use factory
var cryptoManager2 = factory.create(options);
cryptoManager1.cipher('mytext')
.then(function(res) {
console.log(res);
});Documentation
Construction
A Crypton instance can be created using factory or using the new keyword.
var factory = require('crypton');
var cryptonManager1 = factory.create();
//or
var Crypton = require('crypton').Crypton;
var cryptonManager2 = new Crypton();new Crypton( [options] ) : Object
The crypton module can be initialized with a configuration object.
Arguments
[options] {Object} Optional configurationReturns
{Object} Get an instanceThe configuration object allows you to overrides default values. If you don't specify any configuration, it uses a default object:
{
crypto: {
secretKey: 'o!rDE(Qbrq7u4OV',
algorithm: 'AES-256-CBC',
inputEncoding: 'utf8', //utf8|base64|hex
outputEncoding: 'base64' //utf8|base64|hex
},
bcrypt: {
saltRounds: 5 //the cost of processing the data
}
}Methods
cipher( text, [options] ) : Promise( string )
Cipher a text with crypto. The operation is reversible. Options param could be the entire crypto configuration or only an attribute:
{
secretKey: 'o!rDE(Qbrq7u4OV'
}Arguments
text {string} Text to cipher
[options] {object} Overrides configurationReturns
{string} Returns the ciphered textThrows
{CipherCryptonError}decipher( text, [options] ) : Promise( string )
Decipher a ciphered text with crypto. Options param could the entire crypto configuration or only an attribute:
{
secretKey: 'o!rDE(Qbrq7u4OV'
}Arguments
text {string} Text to decipher
[options] {object} Overrides configurationReturns
{string} Returns the deciphered textThrows
{DecipherCryptonError}crypt( text, [options] ) : Promise( string )
Crypt a text with bcrypt. The operation is not reversible. Options param could the entire bcrypt configuration or only an attribute:
{
saltRound: 10
}Arguments
text {string} Text to crypt
[options] {object} Overrides configurationReturns
{string} Returns the crypted textThrows
{EncryptCryptonError}compare( text, ciphered, force, [options] ) : Promise( bool )
Check if the clear text matches with the ciphered text. If force is specified it accepts two ciphered strings to compare. Use this method only with ciphered text. Options param could the entire crypto configuration or only an attribute:
{
secretKey: 'o!rDE(Qbrq7u4OV'
}Arguments
text {string} Text to compare with ciphered
ciphered {string} Ciphered text
force {bool} Force the compare
[options] {object} Overrides configurationReturns
{bool} Returns the result of the matchThrows
{CompareCryptonError}verify( text, crypted ) : Promise( bool )
Check if the clear text matches with the crypted text. Use this method only with crypted text.
Arguments
text {string} Text to compare with crypted
crypted {string} Crypted textReturns
{bool} Returns the result of the matchThrows
{VerifyCryptonError}md5( text ) : Promise( string )
Get md5 hash of a given string.
Arguments
text {string} Text to hashReturns
{string} Returns md5sum in hex formatThrows
{Md5CryptonError}randomBytes( len ) : Promise( string )
Get random bytes.
Arguments
len {int} Bytes lengthReturns
{string} Returns bytes in hex formatThrows
{RandomBytesCryptonError}License
The MIT License
Copyright (c) 2017 Michele Andreoli http://thinkingmik.com