"use strict";
/**
 * Binary Verifier
 * Handles checksum verification for downloaded binaries.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyChecksum = exports.parseChecksum = exports.computeChecksum = void 0;
const fs = __importStar(require("fs"));
const crypto = __importStar(require("crypto"));
const downloader_1 = require("./downloader");
/**
 * Compute SHA256 checksum of file
 */
function computeChecksum(filePath) {
    return new Promise((resolve, reject) => {
        const hash = crypto.createHash('sha256');
        const stream = fs.createReadStream(filePath);
        stream.on('data', (data) => hash.update(data));
        stream.on('end', () => resolve(hash.digest('hex')));
        stream.on('error', reject);
    });
}
exports.computeChecksum = computeChecksum;
/**
 * Parse checksum from checksums.txt content
 */
function parseChecksum(content, binaryName) {
    const lines = content.split('\n');
    for (const line of lines) {
        // Format: "hash  filename" or "hash filename"
        const parts = line.trim().split(/\s+/);
        if (parts.length >= 2 && parts[1] === binaryName) {
            return parts[0].toLowerCase();
        }
    }
    return null;
}
exports.parseChecksum = parseChecksum;
/**
 * Verify file checksum against checksums.txt
 */
async function verifyChecksum(filePath, binaryName, checksumsUrl, verbose = false) {
    // Download checksums.txt
    const checksumsContent = await (0, downloader_1.fetchText)(checksumsUrl, verbose);
    // Parse expected checksum
    const expectedHash = parseChecksum(checksumsContent, binaryName);
    if (!expectedHash) {
        throw new Error(`Checksum not found for ${binaryName} in checksums.txt`);
    }
    // Compute actual checksum
    const actualHash = await computeChecksum(filePath);
    return {
        valid: actualHash === expectedHash,
        expected: expectedHash,
        actual: actualHash,
    };
}
exports.verifyChecksum = verifyChecksum;
//# sourceMappingURL=verifier.js.map