Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Bit of code you can run in your developer toolbar or on https://jsfiddle.net .
*
* Fill in the characteristics of your own 'public file' solution and acceptance criteria.
*/
(function(){
'use strict';
// dec=10,hex=16,[A-Z0-9]=36, etc.
var CHARACTER_COMBINATIONS = 16,
NUMBER_OF_CHARACTERS= 6,
TOTAL_COMBINATIONS = Math.pow(CHARACTER_COMBINATIONS, NUMBER_OF_CHARACTERS),
// How many files do you expect to host?
EXPECTED_NUMBER_OF_FILES=1,
// At what percentage does it become unacceptable.
MINIMUM_PERCENTAGE=50,
// How many tries per second could an attacker get away with?
TRIES_PER_SECOND=700,
// A years worth of seconds
MAX_SECONDS = 60 * 60 * 24 * 365;
var minSeconds = 0,
maxSeconds = MAX_SECONDS,
currentSeconds,
currentPercentage,
triesAtThisTime;
// Do binary search in the given range.
while (minSeconds <= maxSeconds) {
currentSeconds = (minSeconds + maxSeconds) / 2 | 0;
triesAtThisTime = TRIES_PER_SECOND * currentSeconds;
if ((TOTAL_COMBINATIONS - triesAtThisTime) <= EXPECTED_NUMBER_OF_FILES) {
currentPercentage = 100;
}
else {
currentPercentage = EXPECTED_NUMBER_OF_FILES / (TOTAL_COMBINATIONS - triesAtThisTime) * 100;
}
if (currentPercentage < MINIMUM_PERCENTAGE) {
minSeconds = currentSeconds + 1;
}
else if (currentPercentage > MINIMUM_PERCENTAGE) {
maxSeconds = currentSeconds - 1;
}
}
if (currentSeconds >= MAX_SECONDS) {
console.log('Above acceptance criteria');
}
else {
console.log('Unacceptable. A brute force attack achieves ' + MINIMUM_PERCENTAGE + '% chance of a hit in ' + currentSeconds + ' seconds');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment