Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created June 22, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timruffles/ecc7805a3e844c74076a388c7c70cf72 to your computer and use it in GitHub Desktop.
Save timruffles/ecc7805a3e844c74076a388c7c70cf72 to your computer and use it in GitHub Desktop.
empirical birthday paradox calculator - for hashes
const hashBits = parseInt(process.env.bits || 32);
const hashValues = Math.pow(2, hashBits);
const checkPoints = Array.from({ length: 9 }, (e, i) => 1 - (i+1)/10);
for(var uniqueHashValuesRemaining = hashValues, p = 1;
p > 0.1;
uniqueHashValuesRemaining -= 1, p *= uniqueHashValuesRemaining/hashValues
) {
if(p < checkPoints[0]) {
checkPoints.shift();
console.log("after", hashValues - uniqueHashValuesRemaining, "hashes, p(collision) = ", ((1 - p) * 100).toFixed(0), "%");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment