Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated hashStream #45

Merged
merged 4 commits into from Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
});
});