Use hash stream instead of crypto.createHash
crypto.createHash is a write stream but not a PassThrough stream!
This commit is contained in:
@@ -19,6 +19,7 @@ const assert = require('assert'),
|
|||||||
debug = require('debug')('box:backupformat/rsync'),
|
debug = require('debug')('box:backupformat/rsync'),
|
||||||
{ EncryptStream } = require('../hush.js'),
|
{ EncryptStream } = require('../hush.js'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
|
HashStream = require('../hash-stream.js'),
|
||||||
hush = require('../hush.js'),
|
hush = require('../hush.js'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
paths = require('../paths.js'),
|
paths = require('../paths.js'),
|
||||||
@@ -52,7 +53,7 @@ async function addFile(sourceFile, encryption, uploader, progressCallback) {
|
|||||||
progressCallback({ message: `Uploading ${sourceFile}: ${transferred}M@${speed}MBps` }); // 0M@0MBps looks wrong
|
progressCallback({ message: `Uploading ${sourceFile}: ${transferred}M@${speed}MBps` }); // 0M@0MBps looks wrong
|
||||||
});
|
});
|
||||||
|
|
||||||
const hash = crypto.createHash('sha256');
|
const hash = new HashStream();
|
||||||
|
|
||||||
let pipeline = null;
|
let pipeline = null;
|
||||||
if (encryption) {
|
if (encryption) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const assert = require('assert'),
|
|||||||
debug = require('debug')('box:backupformat/tgz'),
|
debug = require('debug')('box:backupformat/tgz'),
|
||||||
{ DecryptStream, EncryptStream } = require('../hush.js'),
|
{ DecryptStream, EncryptStream } = require('../hush.js'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
|
HashStream = require('../hash-stream.js'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
ProgressStream = require('../progress-stream.js'),
|
ProgressStream = require('../progress-stream.js'),
|
||||||
promiseRetry = require('../promise-retry.js'),
|
promiseRetry = require('../promise-retry.js'),
|
||||||
@@ -141,7 +142,7 @@ async function tarPack(dataLayout, encryption, uploader, progressCallback) {
|
|||||||
|
|
||||||
const pack = tar.pack();
|
const pack = tar.pack();
|
||||||
|
|
||||||
const hash = crypto.createHash('sha256');
|
const hash = new HashStream();
|
||||||
|
|
||||||
let pipeline = null;
|
let pipeline = null;
|
||||||
if (encryption) {
|
if (encryption) {
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
'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;
|
||||||
Reference in New Issue
Block a user