A collection of helper functions that encrypt, decrypt, and hash strings and files based on NodeJS's native crypto
module.
This module can be used to create ciphers and decipher them. It makes dealing with Node's crypto
module a lot easier.
npm install --save encryptionhelper
var EncryptionHelper = require('encryptionhelper');
var hash = EncryptionHelper.hash('some buffer/string data', 'md5');
// hash is equal to md5 of the given string
// Also supports MD5, SHA1, SHA256, and many more (based on whatever NodeJS natively supports-- use `openssl list-message-digest-algorithms` to display the avaiable digest algorithms on your machine)
var fileStream = EncryptionHelper.hashFile('path/to/file', 'md5', function (err, res) {
// err is any error that occured
// res is the md5 hash of the file at path/to/file
});
var myKey = 'you-will-never-guess';
var cipher = EncryptionHelper.cipher(myKey, 'some buffer/string data', 'aes256');
// Creates a aes256-based cipher using the key provided
// Supports more than just the AES256 algo-- supports all the algo's NodeJS's crypto module supports
// Use `openssl list-cipher-algorithms` to display the available cipher algorithms on your machine
var originalString = EncryptionHelper.decipher(myKey, cipher);
// originalString === 'some buffer/string data'
var fileStream = EncryptionHelper.cipherFile(myKey, 'path/to/file', function (err, res) {
// err is any error that occured
// res is the ciphered/encrypted version of the file's contents
});
var fileStream = EncryptionHelper.decipherFile(myKey, 'path/to/file', function (err, res) {
// err is any error that occured
// res is the deciphered/unencrypted version of the file's contents
});
Calculates and returns a checksum String
or Buffer
, the digest of all of the passed data
to be hashed.
Parameters:
data
-String
orBuffer
- represents the data to be used to create the hashalgorithm
-String
- represents the algorithm to be used to create the digest. Useopenssl list-message-digest-algorithms
orconsole.log(require('crypto').getHashes());
to display the avaiable digest algorithms on your machine. Defaults to'md5'
.outputEncoding
-String
- represents the encoding of the output produced by this function. This can be'hex'
,'binary'
, or'base64'
. If encoding is passed in as null, then a buffer is returned. Defaults to'hex'
.inputEncoding
-String
- represents the encoding of the inputdata
. This can be'utf8'
,'ascii'
, or'binary'
. If encoding is passed in as null, then a buffer is expected. Defaults to'utf8'
.
Returns: a hashed String
(the md5, sha1, sha256 ... checksum)
Calculates and returns a checksum String
or Buffer
, the digest of the passed in file
to be hashed.
Parameters:
file
-String
- represents the path to the file to be used to create the hashalgorithm
-String
- represents the algorithm to be used to create the digest. Useopenssl list-message-digest-algorithms
orconsole.log(require('crypto').getHashes());
to display the avaiable digest algorithms on your machine. Defaults to'md5'
.outputEncoding
-String
- represents the encoding of the output produced by this function. This can be'hex'
,'binary'
, or'base64'
. If encoding is passed in as null, then a buffer is returned. Defaults to'hex'
.inputEncoding
-String
- represents the encoding of the inputdata
. This can be'utf8'
,'ascii'
, or'binary'
. If encoding is passed in as null, then a buffer is expected. Defaults to'utf8'
.cb
-Function
- A callback to run afterwords. The method signature looks like:function (err, hash){ }
Returns: an open file stream
Ciphers/encrypts data
with the given key
.
Parameters:
key
-String
- a secret key (eg:'you-will-never-guess'
)data
-String
orBuffer
- represents the data to be used to create the cipheralgorithm
-String
- represents the algorithm to be used to create the cipher. Useopenssl list-cipher-algorithms
orconsole.log(require('crypto').getCiphers());
to display the avaiable cipher algorithms on your machine. Defaults to'md5'
.outputEncoding
-String
- represents the encoding of the output produced by this function. This can be'hex'
,'binary'
, or'base64'
. If encoding is passed in as null, then a buffer is returned. Defaults to'hex'
.inputEncoding
-String
- represents the encoding of the inputdata
. This can be'utf8'
,'ascii'
, or'binary'
. If encoding is passed in as null, then a buffer is expected. Defaults to'utf8'
.
Returns: a ciphered/encrypted String
or Buffer
Deciphers previously encrypted ciphered
that was ciphered with the given key
.
Parameters:
key
-String
- a secret key (eg:'you-will-never-guess'
)ciphered
-String
orBuffer
- represents the ciphered data to be decipheredalgorithm
-String
- represents the algorithm which was used to create the cipher. Useopenssl list-cipher-algorithms
orconsole.log(require('crypto').getCiphers());
to display the avaiable cipher algorithms on your machine. Defaults to'md5'
.outputEncoding
-String
- represents the encoding of the output produced by this function. This can be'utf8'
,'ascii'
, or'binary'
. If encoding is passed in as null, then a buffer is returned. Defaults to'utf8'
.inputEncoding
-String
- represents the encoding of the inputciphered
. This can be'hex'
,'binary'
, or'base64'
. If encoding is passed in as null, then a buffer is expected. Defaults to'hex'
.
Returns: the deciphered/unencrypted String
or Buffer
Also contains functions to cipher/decipher from files
- HMAC support
- CipherIV support
- DiffieHellman support
- Unit Tests
- Pipe files directly into Hash/Cipher streams
(The MIT License)
Copyright (c) 2013 Tarun Chaudhry <tarunc92@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.