"use strict";
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.installCrypto = installCrypto;
const crypto = __importStar(require("crypto"));
const evaluator_1 = require("../language/evaluator");
function asString(obj) {
    return obj instanceof evaluator_1.StringObj ? obj.value : null;
}
/**
 * Cryptographic hashing builtins backed by Node crypto.
 */
function installCrypto(env) {
    env.set("sha256", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("sha256: want 1 string");
        }
        const s = asString(args[0]);
        if (s === null) {
            return new evaluator_1.ErrorObj("sha256: want string");
        }
        return new evaluator_1.StringObj(crypto.createHash("sha256").update(s, "utf8").digest("hex"));
    }));
    env.set("sha512", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("sha512: want 1 string");
        }
        const s = asString(args[0]);
        if (s === null) {
            return new evaluator_1.ErrorObj("sha512: want string");
        }
        return new evaluator_1.StringObj(crypto.createHash("sha512").update(s, "utf8").digest("hex"));
    }));
    env.set("md5", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("md5: want 1 string");
        }
        const s = asString(args[0]);
        if (s === null) {
            return new evaluator_1.ErrorObj("md5: want string");
        }
        return new evaluator_1.StringObj(crypto.createHash("md5").update(s, "utf8").digest("hex"));
    }));
    env.set("hmac_sha256", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 2) {
            return new evaluator_1.ErrorObj("hmac_sha256: want key, data");
        }
        const key = asString(args[0]);
        const data = asString(args[1]);
        if (key === null || data === null) {
            return new evaluator_1.ErrorObj("hmac_sha256: want strings");
        }
        return new evaluator_1.StringObj(crypto.createHmac("sha256", key).update(data, "utf8").digest("hex"));
    }));
    env.set("random_bytes_hex", new evaluator_1.BuiltinObj((...args) => {
        let n = 16;
        if (args.length === 1 && args[0] && "value" in args[0]) {
            n = Number(args[0].value);
        }
        if (!Number.isFinite(n) || n < 0 || n > 65536) {
            return new evaluator_1.ErrorObj("random_bytes_hex: invalid length");
        }
        return new evaluator_1.StringObj(crypto.randomBytes(n).toString("hex"));
    }));
}
//# sourceMappingURL=crypto.js.map