Created
January 9, 2012 03:21
-
-
Save mashihua/1580853 to your computer and use it in GitHub Desktop.
Hash collision in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$size = pow(2, 16); // 16 is just an example, could also be 15 or 17 | |
$startTime = microtime(true); | |
$array = array(); | |
for ($key = 0, $maxKey = ($size - 1) * $size; $key <= $maxKey; $key += $size) { | |
$array[$key] = 0; | |
} | |
$endTime = microtime(true); | |
echo 'Inserting ', $size, ' evil elements took ', $endTime - $startTime, ' seconds', "\n"; | |
$startTime = microtime(true); | |
$array = array(); | |
for ($key = 0, $maxKey = $size - 1; $key <= $maxKey; ++$key) { | |
$array[$key] = 0; | |
} | |
$endTime = microtime(true); | |
echo 'Inserting ', $size, ' good elements took ', $endTime - $startTime, ' seconds', "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// entries with collisions in PHP hashtable hash function | |
$a = array( | |
'0' => 'Ez', | |
'1' => 'FY', | |
'2' => 'G8', | |
'3' => 'H' . chr(23), | |
'4' => 'D' . chr(122+33), | |
); | |
// how long should the payload be | |
$length = 7; | |
$size = count($a); | |
$array = array(); | |
$max = pow($size,$length); | |
$startTime = microtime(true); | |
for ($i = 0; $i < $max; $i++) { | |
$s = str_pad(base_convert($i, 10, $size), $length, '0', STR_PAD_LEFT); | |
$array[strtr($s, $a)] = 0; | |
} | |
$endTime = microtime(true); | |
echo 'Inserting ', $size, ' evil elements took ', $endTime - $startTime, ' seconds', "\n"; | |
$startTime = microtime(true); | |
$array = array(); | |
for ($i = 0; $i < $max; $i++) { | |
$array["abc".$key] = 0; | |
} | |
$endTime = microtime(true); | |
echo 'Inserting ', $size, ' good elements took ', $endTime - $startTime, ' seconds', "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment