import { Buffer } from 'node:buffer';
import { constants, createCipheriv, createDecipheriv, generateKeyPairSync, privateEncrypt, publicDecrypt, randomBytes, } from 'node:crypto';
const algorithm = 'aes-128-cbc';
const formatB64 = 'base64';
const padding = constants.RSA_PKCS1_PADDING;
export function generateSessionKey(key) {
    const initVector = randomBytes(16);
    const sessionKey = randomBytes(16);
    const ivB64 = initVector.toString(formatB64);
    const sessionb64Encrypted = privateEncrypt({
        key,
        padding,
    }, sessionKey).toString(formatB64);
    return {
        sessionKey,
        ivSessionKey: `${ivB64}:${sessionb64Encrypted}`,
    };
}
export function encryptSourceV2(source, sessionKey, ivSessionKey) {
    const [ivB64] = ivSessionKey.split(':');
    const initVector = Buffer.from(ivB64, formatB64);
    const cipher = createCipheriv(algorithm, sessionKey, initVector);
    cipher.setAutoPadding(true);
    const encryptedData = Buffer.concat([cipher.update(source), cipher.final()]);
    return encryptedData;
}
export function decryptSourceV2(source, ivSessionKey, key) {
    const [ivB64, sessionb64Encrypted] = ivSessionKey.split(':');
    const sessionKey = publicDecrypt({
        key,
        padding,
    }, Buffer.from(sessionb64Encrypted, formatB64));
    // ivB64 to uft-8
    const initVector = Buffer.from(ivB64, formatB64);
    // console.log('\nSessionB64', sessionB64)
    const decipher = createDecipheriv(algorithm, sessionKey, initVector);
    decipher.setAutoPadding(true);
    const decryptedData = Buffer.concat([decipher.update(source), decipher.final()]);
    return decryptedData;
}
export function encryptChecksumV2(checksum, key) {
    const checksumEncrypted = privateEncrypt({
        key,
        padding,
    }, Buffer.from(checksum, formatB64)).toString(formatB64);
    return checksumEncrypted;
}
export function createRSA(format = 'pem', keySize = 2048) {
    const { publicKey, privateKey } = generateKeyPairSync('rsa', {
        // The standard secure default length for RSA keys is 2048 bits
        modulusLength: keySize,
    });
    // Generate RSA key pair
    if (format === 'pem') {
        return {
            publicKey: publicKey.export({
                type: 'pkcs1',
                format: 'pem',
            }),
            privateKey: privateKey.export({
                type: 'pkcs1',
                format: 'pem',
            }),
        };
    }
    else {
        return {
            publicKey: publicKey.export({
                type: 'spki',
                format: 'der',
            }).toString('base64'),
            privateKey: privateKey.export({
                type: 'pkcs1',
                format: 'pem',
            }).toString('base64'),
        };
    }
}
export function decryptChecksumV2(checksum, key) {
    const checksumDecrypted = publicDecrypt({
        key,
        padding,
    }, Buffer.from(checksum, formatB64)).toString(formatB64);
    return checksumDecrypted;
}
export function createRSAV2() {
    const { publicKey, privateKey } = generateKeyPairSync('rsa', {
        modulusLength: 2048,
    });
    // Generate RSA key pair
    return {
        publicKey: publicKey.export({
            type: 'pkcs1',
            format: 'pem',
        }),
        privateKey: privateKey.export({
            type: 'pkcs1',
            format: 'pem',
        }),
    };
}
//# sourceMappingURL=cryptoV2.js.map