forked from brave/brave-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculateFileChecksum.js
More file actions
28 lines (26 loc) · 1.1 KB
/
calculateFileChecksum.js
File metadata and controls
28 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright (c) 2019 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
const crypto = require('crypto')
const fs = require('fs')
module.exports = function CalculateFileChecksum(filePath, algorithm = 'sha256') {
return new Promise((resolve, reject) => {
try {
const checksumGenerator = crypto.createHash(algorithm);
const fileStream = fs.createReadStream(filePath)
fileStream.on('error', function (err) {
err.message = `CalculateFileChecksum error in FileStream at path "${filePath}": ${err.message}`
reject(err)
})
checksumGenerator.once('readable', function () {
const checksum = checksumGenerator.read().toString('hex')
resolve(checksum)
})
fileStream.pipe(checksumGenerator)
} catch (err) {
err.message = `CalculateFileChecksum error using algorithm "${algorithm}" at path "${filePath}": ${err.message}`
reject(err);
}
});
}