Skip to content

Commit

Permalink
Merge pull request #45 from segayuu/deprecated-hashStream
Browse files Browse the repository at this point in the history
Deprecated hashStream
  • Loading branch information
segayuu committed Nov 5, 2018
2 parents c7cb519 + 8296e97 commit 17a9b7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -67,6 +67,7 @@ hash('123456');
```

### HashStream()
**\[deprecated\]** use `createSha1Hash()`.

Generates SHA1 hash with a transform stream.

Expand All @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions lib/hash.js
Expand Up @@ -5,10 +5,17 @@ const crypto = require('crypto');

const 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);
Expand All @@ -24,9 +31,10 @@ HashStream.prototype._flush = function(callback) {
};

exports.hash = content => {
const hash = crypto.createHash(ALGORITHM);
const hash = createSha1Hash();
hash.update(content);
return hash.digest();
};

exports.HashStream = HashStream;
exports.createSha1Hash = createSha1Hash;
19 changes: 19 additions & 0 deletions test/scripts/hash.js
Expand Up @@ -27,4 +27,23 @@ describe('hash', () => {

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));
});
});

0 comments on commit 17a9b7b

Please sign in to comment.