25 lines
774 B
TypeScript
25 lines
774 B
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import crypto from 'crypto';
|
|
|
|
export const takeChecksum = (file: string): Promise<string> => {
|
|
const hash = crypto.createHash('sha512');
|
|
return new Promise((resolve, reject) => {
|
|
const readStream = fs.createReadStream(path.resolve(file));
|
|
readStream.on('data', (chunk) => {
|
|
try {
|
|
hash.update(chunk);
|
|
} catch(e: unknown) {
|
|
reject(e);
|
|
}
|
|
});
|
|
readStream.on('close', () => resolve(hash.digest('hex')));
|
|
readStream.on('error', (err) => reject(err));
|
|
});
|
|
};
|
|
|
|
export const checkChecksum = async (file: string, checksum: string): Promise<boolean> => {
|
|
const fileSum = await takeChecksum(file);
|
|
return crypto.timingSafeEqual(Buffer.from(fileSum), Buffer.from(checksum));
|
|
};
|