Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/crypto.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as crypto from 'crypto';
7
import * as fs from 'fs';
8
import { createSingleCallFunction } from '../common/functional.js';
9
10
export async function checksum(path: string, sha256hash: string | undefined): Promise<void> {
11
const checksumPromise = new Promise<string | undefined>((resolve, reject) => {
12
const input = fs.createReadStream(path);
13
const hash = crypto.createHash('sha256');
14
input.pipe(hash);
15
16
const done = createSingleCallFunction((err?: Error, result?: string) => {
17
input.removeAllListeners();
18
hash.removeAllListeners();
19
20
if (err) {
21
reject(err);
22
} else {
23
resolve(result);
24
}
25
});
26
27
input.once('error', done);
28
input.once('end', done);
29
hash.once('error', done);
30
hash.once('data', (data: Buffer) => done(undefined, data.toString('hex')));
31
});
32
33
const hash = await checksumPromise;
34
35
if (hash !== sha256hash) {
36
throw new Error('Hash mismatch');
37
}
38
}
39
40