"use strict";
/**
 * Browser-only Encryptor
 * Uses Web Crypto API (hardware accelerated)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Encryptor = void 0;
const utils_base_1 = require("../../../utils/utils.base.js");
const _stream_1 = require("../../stream/index.js");
// Helper to convert number to little-endian Uint8Array
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 = {
    /**
     * Calculate hash using Web Crypto API
     */
    async hash(algorithm, ...buffers) {
        const data = (0, _stream_1.concatUint8Arrays)(buffers);
        const hashBuffer = await crypto.subtle.digest(algorithm, new Uint8Array(data));
        return new Uint8Array(hashBuffer);
    },
    /**
     * Convert password to hash
     */
    async convertPasswordToHash(password, hashAlgorithm, saltValue, spinCount) {
        const passwordBuffer = (0, utils_base_1.stringToUtf16Le)(password);
        const saltBuffer = (0, utils_base_1.base64ToUint8Array)(saltValue);
        let key = await this.hash(hashAlgorithm, saltBuffer, passwordBuffer);
        for (let i = 0; i < spinCount; i++) {
            key = await this.hash(hashAlgorithm, key, uint32ToLe(i));
        }
        return (0, utils_base_1.uint8ArrayToBase64)(key);
    },
    /**
     * Generate cryptographically strong random bytes
     */
    randomBytes(size) {
        const bytes = new Uint8Array(size);
        crypto.getRandomValues(bytes);
        return bytes;
    }
};
exports.Encryptor = Encryptor;
