"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.encrypt = encrypt;
exports.decrypt = decrypt;
exports.hashPassword = hashPassword;
exports.comparePassword = comparePassword;
exports.generateOtp = generateOtp;
const crypto_1 = require("crypto");
const bcrypt = require("bcrypt");
const ALGORITHM = 'aes-256-cbc';
const IV_LENGTH = 16;
function encrypt(text, key) {
    const derivedKey = Buffer.from(key.padEnd(32, '0').slice(0, 32), 'utf8');
    const iv = (0, crypto_1.randomBytes)(IV_LENGTH);
    const cipher = (0, crypto_1.createCipheriv)(ALGORITHM, derivedKey, iv);
    let encrypted = cipher.update(text, 'utf8', 'hex');
    encrypted += cipher.final('hex');
    return iv.toString('hex') + ':' + encrypted;
}
function decrypt(encrypted, key) {
    const parts = encrypted.split(':');
    const iv = Buffer.from(parts.shift(), 'hex');
    const encryptedText = parts.join(':');
    const derivedKey = Buffer.from(key.padEnd(32, '0').slice(0, 32), 'utf8');
    const decipher = (0, crypto_1.createDecipheriv)(ALGORITHM, derivedKey, iv);
    let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}
async function hashPassword(password) {
    const saltRounds = 12;
    return bcrypt.hash(password, saltRounds);
}
async function comparePassword(password, hash) {
    return bcrypt.compare(password, hash);
}
function generateOtp() {
    return (0, crypto_1.randomInt)(100000, 999999).toString();
}
