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 crypton
Example
Let's start! Include in your node application crypton
module:
//require objectvar Crypton = Crypton;//or require factoryvar factory = ; //create optionsvar options = crypto: secretKey: 'o!rDE(Qbrq7u4OV' algorithm: 'AES-256-CBC' inputEncoding: 'utf8' outputEncoding: 'base64' bcrypt: saltRounds: 5 ; //create an instancevar cryptoManager1 = options;//or use factoryvar cryptoManager2 = factory; cryptoManager1;
Documentation
Construction
A Crypton
instance can be created using factory or using the new
keyword.
var factory = ;var cryptonManager1 = factory;//orvar Crypton = Crypton;var cryptonManager2 = ;
new Crypton( [options] ) : Object
The crypton
module can be initialized with a configuration object.
Arguments
options Object Optional configuration
Returns
Object Get an instance
The 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 configuration
Returns
{string} Returns the ciphered text
Throws
{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 configuration
Returns
{string} Returns the deciphered text
Throws
{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 configuration
Returns
{string} Returns the crypted text
Throws
{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 cipheredciphered {string} Ciphered textforce {bool} Force the compare[options] {object} Overrides configuration
Returns
{bool} Returns the result of the match
Throws
{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 cryptedcrypted {string} Crypted text
Returns
{bool} Returns the result of the match
Throws
{VerifyCryptonError}
md5( text ) : Promise( string )
Get md5 hash of a given string.
Arguments
text {string} Text to hash
Returns
{string} Returns md5sum in hex format
Throws
{Md5CryptonError}
randomBytes( len ) : Promise( string )
Get random bytes.
Arguments
len {int} Bytes length
Returns
{string} Returns bytes in hex format
Throws
{RandomBytesCryptonError}
License
The MIT License
Copyright (c) 2017 Michele Andreoli http://thinkingmik.com