39 lines
749 B
JavaScript
39 lines
749 B
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const crypto = require('crypto'),
|
||
|
|
stream = require('stream'),
|
||
|
|
TransformStream = stream.Transform;
|
||
|
|
|
||
|
|
class HashStream extends TransformStream {
|
||
|
|
#hasher;
|
||
|
|
#digest;
|
||
|
|
#size;
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
super();
|
||
|
|
this.#hasher = crypto.createHash('sha256');
|
||
|
|
this.#digest = null;
|
||
|
|
this.#size = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
digest() {
|
||
|
|
return this.#digest;
|
||
|
|
}
|
||
|
|
|
||
|
|
size() {
|
||
|
|
return this.#size;
|
||
|
|
}
|
||
|
|
|
||
|
|
_transform(chunk, encoding, callback) {
|
||
|
|
this.#hasher.update(chunk);
|
||
|
|
this.#size += chunk.length;
|
||
|
|
callback(null, chunk);
|
||
|
|
}
|
||
|
|
|
||
|
|
_flush(callback) {
|
||
|
|
this.#digest = this.#hasher.digest('hex');
|
||
|
|
callback(null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
exports = module.exports = HashStream;
|