-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcalcHash.js
More file actions
31 lines (24 loc) · 777 Bytes
/
calcHash.js
File metadata and controls
31 lines (24 loc) · 777 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
26
27
28
29
30
31
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const calculateHash = async () => {
const filePath = path.resolve(
__dirname,
'files',
'fileToCalculateHashFor.txt',
);
return new Promise((resolve, reject) => {
const hash = createHash('sha256');
const stream = createReadStream(filePath);
stream.on('error', () => reject(new Error('FS operation failed')));
stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => {
console.log(hash.digest('hex'));
resolve();
});
});
};
await calculateHash();