Skip to content

Instantly share code, notes, and snippets.

@natmchugh
Created May 29, 2014 10:10
Show Gist options
  • Save natmchugh/d9d4cd3118e4916b931e to your computer and use it in GitHub Desktop.
Save natmchugh/d9d4cd3118e4916b931e to your computer and use it in GitHub Desktop.
Quick check of the table at http://en.wikipedia.org/wiki/Birthday_attack
<?php
$hashes = [];
$count = 0;
while (true) {
$randBytes = openssl_random_pseudo_bytes(100);
++$count;
$hash = hash('md5', $randBytes);
$smallerHash = substr($hash, 0, 8);
if (isset($hashes[$smallerHash])) {
$collision = base64_encode($hashes[$smallerHash]);
printf("Collision found after %s, %s and %s both hash to %s", $count, $collision, base64_encode($randBytes), $smallerHash);
echo PHP_EOL;
break;
}
$hashes[$smallerHash] = $randBytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment