import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { doInitialRecursions_keystretch, execRound_getNextHash, } from "../common/encrypt-decrypt-common.mjs"; import { AlphabetIndexingMode, HashAlgorithm, SaltStrategy } from "../types.mjs"; /** * Does the actual encryption from hexEncodedData with the given params. * * @returns encryptedData */ export async function encryptFromHex_stream({ hexEncodedData, initialRecursions, recursionsPerHash, salt, saltStrategy, secret, hashAlgorithm, encryptedDataDelimiter, indexingMode, }: { hexEncodedData: string, initialRecursions: number, recursionsPerHash: number, salt: string, saltStrategy: SaltStrategy, secret: string, hashAlgorithm: HashAlgorithm, encryptedDataDelimiter: string, indexingMode: AlphabetIndexingMode, }): Promise { const lc = `[${encryptFromHex_stream.name}]`; try { // set up "prevHash" as a starting point, similar to key-stretching let prevHash = await doInitialRecursions_keystretch({ secret, initialRecursions, salt, saltStrategy: saltStrategy!, hashAlgorithm: hashAlgorithm!, }); // console.log(`${lc} first prevHash: ${prevHash}`); const getIndex: (alphabet: string, hexChar: string) => number = indexingMode === 'indexOf' ? (alphabet: string, hexChar: string) => { return alphabet.indexOf(hexChar) } : (alphabet: string, hexChar: string) => { return alphabet.lastIndexOf(hexChar) }; // we have our prevHash starting point, so now we can iterate through the data let encryptedDataIndexes: number[] = []; for (let i = 0; i < hexEncodedData.length; i++) { // this is the character of data that we want to map to an index into the generated alphabet const hexCharFromData: string = hexEncodedData[i]; let alphabet: string = ""; let hash: string; while (!alphabet.includes(hexCharFromData)) { hash = await execRound_getNextHash({ count: recursionsPerHash, prevHash, salt, saltStrategy, hashAlgorithm }); alphabet += hash!; prevHash = hash; } // we now have the alphabet, so find the index of hex character const charIndex = getIndex(alphabet, hexCharFromData); encryptedDataIndexes.push(charIndex); } const encryptedData = encryptedDataIndexes.join(encryptedDataDelimiter); return encryptedData; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }