1
>
/*---------------------------------------------------------------------------------------------
crypto.ts
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
>
input.destroy();
20
>
21
>
if (err) {
22
reject(err);
24
>
resolve(result);
25
>
}
26
>
});
27
>
28
>
input.once('error', done);
29
>
input.once('end', done);
30
>
hash.once('error', done);
31
>
hash.once('data', (data: Buffer) => done(undefined, data.toString('hex')));
32
>
});
33
>
34
>
const hash = await checksumPromise;
35
>
36
>
if (hash !== sha256hash) {
37
throw new Error('Hash mismatch');
38
}