forked from tediousjs/setup-sqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.ts
More file actions
25 lines (24 loc) · 756 Bytes
/
crypto.ts
File metadata and controls
25 lines (24 loc) · 756 Bytes
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
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
/**
* Generate a sha256 hash of a file from its path.
* This is used for some debugging info so that sha sums can be confirmed for downloaded assets.
*
* @param {string} path
* @returns {Promise<Buffer>}
*/
export function generateFileHash(path: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
const hash = createHash('sha256');
const fh = createReadStream(path);
fh.on('error', reject);
fh.on('readable', () => {
const data = fh.read();
if (data) {
hash.update(data);
} else {
resolve(hash.digest());
}
});
});
}