crypton

1.0.8 • Public • Published

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 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 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 ciphered
ciphered  {string}  Ciphered text
force     {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 crypted
crypted  {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

Package Sidebar

Install

npm i crypton

Weekly Downloads

9

Version

1.0.8

License

MIT

Last publish

Collaborators

  • thinkingmik