"use strict";
/**
 * Node.js Encryptor - uses native crypto module
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Encryptor = void 0;
const crypto_1 = __importDefault(require("crypto"));
const utils_base_1 = require("../../../utils/utils.base.js");
const _stream_1 = require("../../stream/index.js");
function uint32ToLe(num) {
    const arr = new Uint8Array(4);
    arr[0] = num & 0xff;
    arr[1] = (num >> 8) & 0xff;
    arr[2] = (num >> 16) & 0xff;
    arr[3] = (num >> 24) & 0xff;
    return arr;
}
const Encryptor = {
    hash(algorithm, ...buffers) {
        const algo = algorithm.toLowerCase().replace(/-/g, "");
        const hash = crypto_1.default.createHash(algo);
        hash.update((0, _stream_1.concatUint8Arrays)(buffers));
        return new Uint8Array(hash.digest());
    },
    async convertPasswordToHash(password, hashAlgorithm, saltValue, spinCount) {
        const passwordBuffer = (0, utils_base_1.stringToUtf16Le)(password);
        const saltBuffer = (0, utils_base_1.base64ToUint8Array)(saltValue);
        let key = this.hash(hashAlgorithm, saltBuffer, passwordBuffer);
        for (let i = 0; i < spinCount; i++) {
            key = this.hash(hashAlgorithm, key, uint32ToLe(i));
        }
        return (0, utils_base_1.uint8ArrayToBase64)(key);
    },
    randomBytes(size) {
        return new Uint8Array(crypto_1.default.randomBytes(size));
    }
};
exports.Encryptor = Encryptor;
