Socket
Socket
Sign inDemoInstall

encryptionhelper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

encryptionhelper - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

tests/test.js

5

History.md

@@ -0,2 +1,7 @@

0.0.2 / 2013-10-06
==================
* Updated readme
* Updated method aliases
0.0.2 / 2013-10-05

@@ -3,0 +8,0 @@ ==================

159

lib/EncryptionHelper.js

@@ -15,10 +15,10 @@ /**

// A helper function to create a one-way md5 hash of a string
encrypt: function(data, encryption, outputEncoding, inputEncoding) {
if (!encryption || 'string' !== typeof encryption || encryption.length < 2) {
encryption = 'md5';
}
if (!outputEncoding) {
hash: function(data, encryption, outputEncoding, inputEncoding) {
encryption = encryption || 'md5';
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'hex';
}
if (!inputEncoding) {
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'utf8';

@@ -32,6 +32,6 @@ }

// A helper function to create a one-way md5 hash of a file
getChecksumOfFile: function(file, algo, outputEncoding, inputEncoding, callback) {
hashFile: function(file, algo, outputEncoding, inputEncoding, callback) {
if (inputEncoding && !callback) {
callback = inputEncoding;
inputEncoding = null;
inputEncoding = undefined;
}

@@ -41,3 +41,3 @@

callback = outputEncoding;
outputEncoding = null;
outputEncoding = undefined;
}

@@ -50,11 +50,9 @@

if (!algo) {
algo = 'md5';
}
algo = algo || 'md5';
if (!outputEncoding) {
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'hex';
}
if (!inputEncoding) {
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'utf8';

@@ -72,8 +70,4 @@ }

fileStream.on('data', function(data) {
hash.update(data);
});
fileStream.on('end', function() {
var result = hash.digest('base64');
var result = hash.digest(outputEncoding);
return callback(null, result);

@@ -86,2 +80,6 @@ });

fileStream.on('data', function(data) {
hash.update(data, inputEncoding);
});
return fileStream;

@@ -94,6 +92,12 @@ },

cipher: function(key, text, algo, outputEncoding, inputEncoding) {
outputEncoding = outputEncoding || 'hex';
inputEncoding = inputEncoding || 'utf8';
algo = algo || 'aes256';
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'hex';
}
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'utf8';
}
var cipher = crypto.createCipher(algo, key);

@@ -107,5 +111,11 @@ return cipher.update(text, inputEncoding, outputEncoding) + cipher.final(outputEncoding);

algo = algo || 'aes256';
outputEncoding = outputEncoding || 'utf8';
inputEncoding = inputEncoding || 'hex';
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'utf8';
}
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'hex';
}
var decipher = crypto.createDecipher(algo, key);

@@ -115,12 +125,109 @@ return decipher.update(encrypted, inputEncoding, outputEncoding) + decipher.final(outputEncoding);

cipherFile: function(key, file, algo, outputEncoding, inputEncoding, cb) {
if (inputEncoding && !callback) {
callback = inputEncoding;
inputEncoding = undefined;
}
if (outputEncoding && !callback) {
callback = outputEncoding;
outputEncoding = undefined;
}
if (algo && !callback) {
callback = algo;
algo = null;
}
if (!algo) {
algo = 'aes256';
}
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'hex';
}
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'utf8';
}
var cipher = crypto.createCipher(algo, key);
var result = '';
var fileStream = fs.createReadStream(file, {
'flags': 'r',
'encoding': inputEncoding,
'mode': 0666,
'bufferSize': 4 * 1024,
});
fileStream.on('end', function() {
result += cipher.final(outputEncoding);
return callback(null, result);
});
fileStream.on('error', function(ex) {
return callback(ex, null);
});
fileStream.on('data', function(data) {
result += cipher.update(data, inputEncoding, outputEncoding);
});
return fileStream;
},
decipherFile: function(key, file, algo, outputEncoding, inputEncoding, cb) {
if (inputEncoding && !callback) {
callback = inputEncoding;
inputEncoding = undefined;
}
if (outputEncoding && !callback) {
callback = outputEncoding;
outputEncoding = undefined;
}
if (algo && !callback) {
callback = algo;
algo = null;
}
if (!algo) {
algo = 'aes256';
}
if (!outputEncoding && outputEncoding !== null) {
outputEncoding = 'hex';
}
if (!inputEncoding && inputEncoding !== null) {
inputEncoding = 'utf8';
}
var decipher = crypto.createDecipher(algo, key);
var result = '';
var fileStream = fs.createReadStream(file, {
'flags': 'r',
'encoding': inputEncoding,
'mode': 0666,
'bufferSize': 4 * 1024,
});
fileStream.on('end', function() {
result += decipher.final(outputEncoding);
return callback(null, result);
});
fileStream.on('error', function(ex) {
return callback(ex, null);
});
fileStream.on('data', function(data) {
result += decipher.update(data, inputEncoding, outputEncoding);
});
return fileStream;
}
};
EncryptionHelper.checksum = EncryptionHelper.getChecksum = EncryptionHelper.encrypt;
EncryptionHelper.encryptFile = EncryptionHelper.checksumFile = EncryptionHelper.getChecksumOfFile;
/**

@@ -127,0 +234,0 @@ * Expose `EncryptionHelper` Library.

2

package.json
{
"name": "encryptionhelper",
"version": "0.0.2",
"version": "0.0.3",
"description": "A collection of helper functions that encrypt, decrypt, and hash strings and files based on the native crypto module",

@@ -5,0 +5,0 @@ "keywords": ["encryption", "decryption", "hashing", "md5", "aes", "hex", "crypto", "files", "strings"],

# EncryptionHelper
A collection of helper functions that encrypt, decrypt, and hash strings and files based on the native crypto module.
This module can be used to create ciphers and decipher them.
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.

@@ -16,7 +16,7 @@ ## Installation

var hash = EncryptionHelper.checksum('some buffer/string data', 'md5');
var hash = EncryptionHelper.hash('some buffer/string data', 'md5');
// hash is equal to md5 of the given string
// Also supports MD5, SHA1, SHA256, and more (based on whatever NodeJS natively supports)
// 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 fileHash = EncryptionHelper.checksumFile('path/to/file', 'md5', function (err, res) {
var fileStream = EncryptionHelper.hashFile('path/to/file', 'md5', function (err, res) {
// err is any error that occured

@@ -26,11 +26,51 @@ // res is the md5 hash of the file at path/to/file

var myKey = 'my_secret_private_key';
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
});
```
## API
#### EncryptionHelper.hash(data, [algorithm, [outputEncoding, [inputEncoding]]]);
Calculates and returns a checksum `String` or `Buffer`, the digest of all of the passed `data` to be hashed.
Parameters:
* `data` - `String` or `Buffer` - represents the data to be used to create the hash
* `algorithm` - `String` - represents the algorithm to be used to create the digest.
Use `openssl list-message-digest-algorithms` or `console.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 input `data`. This can be `'utf8'`, `'ascii'`, or `'binary'`. If encoding is passed in as null, then a buffer is expected. Defaults to `'utf8'`.
Returns: a hash string
#### EncryptionHelper.hashFile(filePath, [algorithm, [outputEncoding, [inputEncoding]]], cb);
Calculates and returns a checksum `String` or `Buffer`, the digest of all of the passed `data` to be hashed.
Parameters:
* `file` - `String` - represents the path to the file to be used to create the hash
* `algorithm` - `String` - represents the algorithm to be used to create the digest.
Use `openssl list-message-digest-algorithms` or `console.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 input `data`. 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
## License

@@ -37,0 +77,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc