From 3b40dae7790d29f175034c5f0618a33f78e9a614 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 18 Oct 2018 10:27:25 +0900 Subject: [PATCH 1/3] Add function: createSha1Hash() - deprecated HashStream - result for crypto.createHash() is stream --- lib/hash.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/hash.js b/lib/hash.js index 4a7197e6..4fe71988 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -5,10 +5,17 @@ var crypto = require('crypto'); var ALGORITHM = 'sha1'; +function createSha1Hash() { + return crypto.createHash(ALGORITHM); +} + +/** + * @deprecated + * createHash() is stream class. + */ function HashStream() { Transform.call(this); - - this._hash = crypto.createHash(ALGORITHM); + this._hash = createSha1Hash(); } require('util').inherits(HashStream, Transform); @@ -23,10 +30,11 @@ HashStream.prototype._flush = function(callback) { callback(); }; -exports.hash = function(content) { - var hash = crypto.createHash(ALGORITHM); +exports.hash = content => { + const hash = createSha1Hash(); hash.update(content); return hash.digest(); }; exports.HashStream = HashStream; +exports.createSha1Hash = createSha1Hash; From 498158863810de2e53c1d3de3d4ec594ff6a0b73 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 18 Oct 2018 10:34:24 +0900 Subject: [PATCH 2/3] Add test: createSha1Hash --- test/scripts/hash.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/scripts/hash.js b/test/scripts/hash.js index 360e2f8a..37fd76a3 100644 --- a/test/scripts/hash.js +++ b/test/scripts/hash.js @@ -27,4 +27,23 @@ describe('hash', function() { stream.read().should.eql(sha1(content)); }); + + it('createSha1Hash', function() { + var _sha1 = hash.createSha1Hash(); + var content = '123456'; + _sha1.update(content); + _sha1.digest().should.eql(sha1(content)); + }); + + it('createSha1Hash - streamMode', function() { + var content1 = '123456'; + var content2 = '654321'; + var stream = hash.createSha1Hash(); + // explicit convert + stream.write(Buffer.from(content1)); + // implicit convert + stream.write(content2); + stream.end(); + stream.read().should.eql(sha1(content1 + content2)); + }); }); From 2bee559f7a105bd154878cf9efa571dff9c70f95 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 18 Oct 2018 10:34:42 +0900 Subject: [PATCH 3/3] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index b536f549..6e49989a 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ hash('123456'); ``` ### HashStream() +**\[deprecated\]** use `createSha1Hash()`. Generates SHA1 hash with a transform stream. @@ -80,6 +81,18 @@ fs.createReadStream('/path/to/file') }); ``` +### createSha1Hash() +return SHA1 hash object. + This is the same as calling `createHash('utf8')` in the node.js native module crypto. + ``` js +const sha1 = createSha1Hash(); + fs.createReadStream('/path/to/file') + .pipe(sha1) + .on('finish', () => { + console.log(sha1.read()); + }); +``` + ### highlight(str, [options]) Syntax highlighting for a code block.