"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.HASH_FUNCTION = void 0;
exports.hash = hash;
exports.validate = validate;
const crypto = __importStar(require("node:crypto"));
const argon2 = __importStar(require("argon2"));
const environmentManager_js_1 = require("./environment/environmentManager.js");
const hdbTerms_ts_1 = require("./hdbTerms.js");
const configuredHashFunction = (0, environmentManager_js_1.get)(hdbTerms_ts_1.CONFIG_PARAMS.AUTHENTICATION_HASHFUNCTION)?.toLowerCase();
const SALT_LENGTH = 16;
const SALT_LENGTH_MD5 = 9;
exports.HASH_FUNCTION = {
    MD5: 'md5',
    SHA256: 'sha256',
    ARGON2ID: 'argon2id',
};
/**
 * Generate a secure salt
 * @param len Length of salt (defaults to SALT_LENGTH)
 */
function generateSalt(len = SALT_LENGTH) {
    const set = '0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ';
    return Array.from(crypto.randomBytes(len))
        .map((x) => set[x % set.length])
        .join('');
}
const hashAlgorithms = {
    [exports.HASH_FUNCTION.MD5]: (password, salt = undefined) => {
        salt = salt ?? generateSalt(SALT_LENGTH_MD5);
        const hashed = crypto
            .createHash(exports.HASH_FUNCTION.MD5)
            .update(password + salt)
            .digest('hex');
        return salt + hashed;
    },
    [exports.HASH_FUNCTION.SHA256]: (password, salt = undefined) => {
        salt = salt ?? generateSalt(SALT_LENGTH);
        const hashed = crypto
            .createHash(exports.HASH_FUNCTION.SHA256)
            .update(password + salt)
            .digest('hex');
        return salt + hashed;
    },
    [exports.HASH_FUNCTION.ARGON2ID]: async (password) => {
        const salt = generateSalt(SALT_LENGTH);
        const hashed = await argon2.hash(password, {
            type: argon2.argon2id,
            salt: Buffer.from(salt),
        });
        return salt + hashed;
    },
};
const validateAlgorithms = {
    [exports.HASH_FUNCTION.MD5]: (storedHash, password) => {
        const salt = storedHash.slice(0, SALT_LENGTH_MD5);
        return storedHash === hashAlgorithms[exports.HASH_FUNCTION.MD5](password, salt);
    },
    [exports.HASH_FUNCTION.SHA256]: (storedHash, password) => {
        const salt = storedHash.slice(0, SALT_LENGTH);
        return storedHash === hashAlgorithms[exports.HASH_FUNCTION.SHA256](password, salt);
    },
    [exports.HASH_FUNCTION.ARGON2ID]: async (storedHash, password) => {
        return await argon2.verify(storedHash.slice(SALT_LENGTH), password);
    },
};
/**
 * Create a hash for the given password (MD5, SHA-256 or argon2id)
 * @param password Plain text password
 * @param algorithm Hashing algorithm to use (default: SHA-256)
 */
function hash(password, algorithm = exports.HASH_FUNCTION[configuredHashFunction?.toUpperCase()] ?? exports.HASH_FUNCTION.SHA256) {
    return hashAlgorithms[algorithm](password);
}
/**
 * Validate a password against a stored hash
 * @param storedHash Previously generated hash
 * @param password Plain text password to validate
 * @param algorithm Hashing algorithm to use (default: SHA-256)
 */
function validate(storedHash, password, algorithm = exports.HASH_FUNCTION[configuredHashFunction?.toUpperCase()] ?? exports.HASH_FUNCTION.SHA256) {
    if (!storedHash)
        return false;
    return validateAlgorithms[algorithm](storedHash, password);
}
//# sourceMappingURL=password.js.map