/** * @module secret respec * * we gotta test our secret */ // import { cwd, chdir, } from 'node:process'; // import { statSync } from 'node:fs'; // import { mkdir, } from 'node:fs/promises'; // import { ChildProcess, exec, ExecException } from 'node:child_process'; import * as pathUtils from 'path'; import { firstOfAll, firstOfEach, ifWe, ifWeMight, iReckon, lastOfAll, lastOfEach, respecfully, respecfullyDear } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; const maam = `[${import.meta.url}]`, sir = maam; import { extractErrorMsg, delay, getSaferSubstring, getTimestampInTicks, getUUID, pretty, HashAlgorithm, clone, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { getCheckIfPasswordProbablyCorrectInfo, passwordProbablyCorrect } from './secret-helper.mjs'; import { hash16816 } from '../other/ibgib-helper.mjs'; import { DEFAULT_PASSWORD_CHECK_SUBSTRING_LENGTH } from './secret-constants.mjs'; /** * for verbose logging */ const logalot = GLOBAL_LOG_A_LOT; // change this when you want to turn off verbose logging const lcFile: string = `[${pathUtils.basename(import.meta.url)}]`; const correctPasswords = [ 'yo', 'hey this is a password', 'jwoiejf oiwejf oiewjf owiefhw9e8hf982y98y2398f 2398f oijsd oifj woeihfew9hg 9we8u98 u34793824798au s9d8u 9a8syg 98ywheg y98uy9*(*^*&#^ $(*&#@( *$& @(#*$&(* #@^T%(YIUdyf 98wye fuwehfiu hweiufh iugh iuh j' ]; const substringLength = DEFAULT_PASSWORD_CHECK_SUBSTRING_LENGTH; const recursionCount = 168; const algorithm: HashAlgorithm = 'SHA-256'; const saltPrependedPerHash = ''; const blockName = `CheckIfPasswordProbablyCorrect`; await respecfully(maam, blockName, async () => { const lc = `${lcFile}[${blockName}]`; if (logalot) { console.time(lc) } await respecfully(maam, `checking passwords`, async () => { await ifWe(maam, `ensure correct password always succeeds`, async () => { // these are to mimic legitimate setting of hash16816 substring and // subsequent legit correct password entries. they should always // succeed, i.e., the hash should always contain the substring. const iterationsToCheck = 10; let failed = false; for (let i = 0; i < correctPasswords.length; i++) { const password = correctPasswords[i]; const fullHash = await hash16816({ s: password, algorithm, recursionCount, saltPrependedPerHash, }); for (let num = 0; num < iterationsToCheck; num++) { const public_checkInfo = await getCheckIfPasswordProbablyCorrectInfo({ password, substringLength, algorithm, saltPrependedPerHash, recursionCount }); const { substring } = public_checkInfo; // manually check (requires us to know internals of // passwordProbablyCorrect fn) if (!fullHash.includes(substring)) { failed = true; break; } // check directly via the fn consumer will use const resProbablyCorrect = await passwordProbablyCorrect({ password, checkInfo: clone(public_checkInfo), }); if (!resProbablyCorrect) { failed = true; break; } } } iReckon(sir, failed).asTo('failed').isGonnaBeFalse(); }); }); if (logalot) { console.timeLog(lc) } await respecfully(maam, `bruteforcing check`, async () => { await ifWe(maam, `try to brute force, we get a collision`, async () => { /** * I've done larger numbers than these, but would be a waste of time to * do more now. change this as needed for custom testing. */ const numBruteForceTests = 3; /** * i just want to average the iterations required to tweak what i should * be using as a substringLength. */ let totalAttemptsForFalseCollisions = 0; for (let i = 0; i < numBruteForceTests; i++) { const correctPassword = correctPasswords[i % correctPasswords.length]; const correctFullHash = await hash16816({ s: correctPassword, algorithm, recursionCount, saltPrependedPerHash, }); /** * when initially creating the secret, the user will store this * known substring in the `secretInfo.passwordProbablyCorrectSubstring`. * from the attacker's POV this substring is public knowledge, and * the attacker is going to try to brute force against this * substring. we want there to be at least one collistion that has * this substring. * * "public" as in the would-be brute forcer knows this (and other * check info) as public metadata */ const public_checkInfo = await getCheckIfPasswordProbablyCorrectInfo({ password: correctPassword, substringLength, algorithm, recursionCount, saltPrependedPerHash, }); const { substring: publicSubstring } = public_checkInfo; /** * some arbitrarily "large" number (really this will be much higher * but we are testing in a respec here). */ const bruteForceAttempts = 1_000_000; let foundFalseCollision = false; for (let j = 0; j < bruteForceAttempts; j++) { const passwordAttempt = j.toString(); const fullHashAttempt = await hash16816({ s: passwordAttempt, algorithm, recursionCount, saltPrependedPerHash, }); if (fullHashAttempt.includes(publicSubstring)) { // their invalid password attempt found a false collision foundFalseCollision = true; totalAttemptsForFalseCollisions += j; console.log(`j: ${j}`); console.log(`publicSubstring: ${publicSubstring}`); console.log(`fullHashAttempt: ${fullHashAttempt}`); console.log(`correctFullHash: ${correctFullHash}`); break; } } iReckon(maam, foundFalseCollision).asTo('foundFalseCollision').isGonnaBeTrue(); // not really for testing, but i want to see the average attempts required let averageAttemptsPerFalseCollision = Math.floor(totalAttemptsForFalseCollisions / (i + 1)); console.log(`averageAttemptsPerFalseCollision: ${averageAttemptsPerFalseCollision}`); } }); }); if (logalot) { console.timeEnd(lc) } });