data-2-hash
Advanced tools
Comparing version 0.3.0 to 1.0.0
@@ -9,4 +9,6 @@ #!/usr/bin/env node | ||
.version(pkg.version) | ||
.usage("<hash-function> <data>") | ||
.usage("[options] <hash-function> <data>") | ||
.option('-l, --list', 'Print all available hashing functions') | ||
.option('-f, --file', 'Hash file') | ||
.option('-t, --text', 'Hash text') | ||
.parse(process.argv); | ||
@@ -18,4 +20,8 @@ | ||
}); | ||
} else if (!!program.file) { | ||
new Hash(program.args[0], program.args[1], true).on('done', function (hash) { | ||
console.log(hash); | ||
}); | ||
} else { | ||
console.log(new Hash(program.args[0], program.args[1]).digest()); | ||
} |
# Changelog | ||
## Edge version | ||
## Version 1.0.0 | ||
- Fix bug with hashing file; | ||
- Improve CLI to work with Hash; | ||
## Version 0.3.0 | ||
@@ -6,0 +9,0 @@ |
120
index.js
var crypto = require('crypto'), | ||
fs = require('fs'); | ||
fs = require('fs'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
util.inherits(Hash, EventEmitter); | ||
/** | ||
@@ -9,20 +13,16 @@ * List of hashes that crypto is supports | ||
*/ | ||
var CRYPTO_SUPPORTED_HASHES = crypto.getHashes(); | ||
var SUPPORTED_HASHES = crypto.getHashes(); | ||
/** | ||
* List of hashes that implemented on my own | ||
* @type {Array} | ||
* @private | ||
*/ | ||
var CUSTOM_SUPPORTED_HASHES = []; | ||
/** | ||
* Hash class | ||
* @param {String} algorithm Algorithm that hashing data | ||
* @param {*} [data] Data to hash | ||
* @param {String} algorithm Algorithm that hash data | ||
* @param {String} [data] Data to hash | ||
* @param {Boolean} [isFile] If data it's file location then should be true | ||
* @constructor | ||
*/ | ||
function Hash(algorithm, data) { | ||
if (CRYPTO_SUPPORTED_HASHES.indexOf(algorithm) === -1 && CUSTOM_SUPPORTED_HASHES.indexOf(algorithm) === -1) { | ||
throw new Error('Unsupported algorithm'); | ||
function Hash(algorithm, data, isFile) { | ||
EventEmitter.apply(this, arguments); | ||
if (SUPPORTED_HASHES.indexOf(algorithm) === -1) { | ||
throw new Error("Unsupported algorithm"); | ||
} | ||
@@ -32,8 +32,14 @@ | ||
if (fs.existsSync(data) && fs.lstatSync(data).isFile()) { | ||
var stream = fs.createReadStream(data); | ||
stream.on('data', function (data) { | ||
// FIXME: callback when hash is updated | ||
this.update(data); | ||
}.bind(this)); | ||
if (isFile) { | ||
if (!(fs.existsSync(data) && fs.lstatSync(data).isFile())) { | ||
throw new Error("File not exists"); | ||
} | ||
fs.createReadStream(data) | ||
.on('data', function (data) { | ||
this.update(data); | ||
}.bind(this)) | ||
.on('end', function () { | ||
this.emit('done', this.digest()); | ||
}.bind(this)); | ||
} else if (data) { | ||
@@ -44,44 +50,40 @@ this.update(data); | ||
Hash.prototype = Object.create({ | ||
constructor: Hash, | ||
/** | ||
* Get current crypto hash instance | ||
* @returns {crypto|*} | ||
* @private | ||
*/ | ||
Hash.prototype._getHash = function () { | ||
return this._hash; | ||
}; | ||
/** | ||
* Get current crypto hash instance | ||
* @returns {crypto|*} | ||
* @private | ||
*/ | ||
_getHash: function () { | ||
return this._hash; | ||
}, | ||
/** | ||
* Set crypto hash instance | ||
* @param {crypto} hash | ||
* @returns {Hash} | ||
* @private | ||
*/ | ||
Hash.prototype._setHash = function (hash) { | ||
this._hash = hash; | ||
return this; | ||
}; | ||
/** | ||
* Set crypto hash instance | ||
* @param {crypto} hash | ||
* @returns {Hash} | ||
* @private | ||
*/ | ||
_setHash: function (hash) { | ||
this._hash = hash; | ||
return this; | ||
}, | ||
/** | ||
* Update data in crypto hash | ||
* @param data | ||
* @returns {Hash} | ||
*/ | ||
Hash.prototype.update = function (data) { | ||
this._getHash().update(data); | ||
return this; | ||
}; | ||
/** | ||
* Update data in crypto hash | ||
* @param data | ||
* @returns {Hash} | ||
*/ | ||
update: function (data) { | ||
this._getHash().update(data); | ||
return this; | ||
}, | ||
/** | ||
* Calculate hash | ||
* @returns {String} | ||
*/ | ||
Hash.prototype.digest = function () { | ||
return this._getHash().digest('hex'); | ||
}; | ||
/** | ||
* Calculate hash | ||
* @returns {String} | ||
*/ | ||
digest: function () { | ||
return this._getHash().digest('hex'); | ||
} | ||
}); | ||
/** | ||
@@ -91,4 +93,4 @@ * List of supported hashes | ||
*/ | ||
Hash.SUPPORTED_HASHES = CRYPTO_SUPPORTED_HASHES.concat(CUSTOM_SUPPORTED_HASHES); | ||
Hash.SUPPORTED_HASHES = SUPPORTED_HASHES; | ||
module.exports = Hash; |
{ | ||
"name": "data-2-hash", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "CLI for crypto module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# data-2-hash [![npm version](https://badge.fury.io/js/data-2-hash.svg)](http://badge.fury.io/js/data-2-hash) [![Build Status](https://travis-ci.org/ghaiklor/data-2-hash.svg)](https://travis-ci.org/ghaiklor/data-2-hash) | ||
CLI interface for `crypto` module. | ||
The main approach of this module is CLI interface for `crypto`. But you can include it and use as another module. | ||
@@ -10,3 +10,3 @@ ## Getting Started | ||
```shell | ||
npm install data-2-hash # Local | ||
npm install data-2-hash --save # Local | ||
npm install -g data-2-hash # Global (will be available CLI) | ||
@@ -19,3 +19,14 @@ ``` | ||
var Hash = require('data-2-hash'); | ||
// Create empty hash instance | ||
var shasum = new Hash('sha'); | ||
console.log(shasum.update('test').digest()); | ||
// Create hash instance with predefined data | ||
console.log(new Hash('md5', 'test').digest()); | ||
// Calculate hash for some file | ||
new Hash('md5', 'file.md', true).on('done', function(digest) { | ||
console.log(digest); | ||
}); | ||
``` | ||
@@ -28,3 +39,4 @@ | ||
```shell | ||
d2h --help | ||
d2h --help # Usage | ||
d2h --list # Print all supported hashes | ||
``` | ||
@@ -31,0 +43,0 @@ |
@@ -10,2 +10,15 @@ var assert = require('assert'), | ||
it('Should properly create empty crypto instance', function () { | ||
var hash; | ||
hash = new Hash('sha1'); | ||
assert.equal(hash.update('test').digest(), 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'); | ||
hash = new Hash('md5'); | ||
assert.equal(hash.update('test').digest(), '098f6bcd4621d373cade4e832627b4f6'); | ||
hash = new Hash('whirlpool'); | ||
assert.equal(hash.update('test').digest(), 'b913d5bbb8e461c2c5961cbe0edcdadfd29f068225ceb37da6defcf89849368f8c6c2eb6a4c4ac75775d032a0ecfdfe8550573062b653fe92fc7b8fb3b7be8d6'); | ||
}); | ||
it('Should properly digest hash', function () { | ||
@@ -23,2 +36,9 @@ var hash; | ||
}); | ||
it('Should properly digest file', function (done) { | ||
new Hash('sha1', 'LICENSE', true).on('done', function (digest) { | ||
assert.equal(digest, 'ac90363e76a56d55aa5113463f78e65aa8c76d79'); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9792
133
0
64
0