src / checksum.ts
import * as fs from "fs";
import { expandPath } from "./securityEnhanced";
export async function fileChecksum(filePath: string, algorithm: "sha256" | "md5" = "sha256"): Promise<string> {
try {
const crypto = await import("crypto");
const hash = crypto.createHash(algorithm);
const stream = fs.createReadStream(expandPath(filePath));
return new Promise<string>((resolve, reject) => {
stream.on("data", (chunk: string | Buffer) => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
stream.on("error", (err) => { stream.destroy(); reject(err); });
});
} catch (err) {
throw new Error(`Failed to compute checksum: ${filePath} — ${(err as Error).message}`);
}
}
src / checksum.ts
import * as fs from "fs";
import { expandPath } from "./securityEnhanced";
export async function fileChecksum(filePath: string, algorithm: "sha256" | "md5" = "sha256"): Promise<string> {
try {
const crypto = await import("crypto");
const hash = crypto.createHash(algorithm);
const stream = fs.createReadStream(expandPath(filePath));
return new Promise<string>((resolve, reject) => {
stream.on("data", (chunk: string | Buffer) => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
stream.on("error", (err) => { stream.destroy(); reject(err); });
});
} catch (err) {
throw new Error(`Failed to compute checksum: ${filePath} — ${(err as Error).message}`);
}
}