compute hash in chunks.

Previously we were loading the entire package file into memory, then
updating the hash. Now we update the hash with chunks of the data as it is read in.
Should fix #259
This commit is contained in:
Thomas Hobson 2021-05-18 21:07:40 +12:00
parent 79c7f471a1
commit c71aea6166
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
1 changed files with 11 additions and 4 deletions

View File

@ -68,10 +68,17 @@ class Package {
logger.debug('Validating checksums');
logger.debug(`Assert sha256(pkg.tar.gz) == ${this.checksum}`);
const cs = crypto
.create_hash('sha256')
.update(fss.readFileSync(pkgpath))
.digest('hex');
const hash = crypto.create_hash('sha256');
const read_stream = fss.create_read_stream(pkgpath);
await new Promise((resolve, reject) => {
read_stream.on('data', chunk => hash.update(chunk));
read_stream.on('end', () => resolve());
read_stream.on('error', error => reject(error))
});
const cs = hash.digest('hex');
if (cs !== this.checksum) {
throw new Error(`Checksum miss-match want: ${val} got: ${cs}`);