Fastest hash for non-cryptographic uses? Fastest hash for non-cryptographic uses? database database

Fastest hash for non-cryptographic uses?


fcn     time  generated hashcrc32:  0.03163  798740135md5:    0.0731   0dbab6d0c841278d33be207f14eeab8bsha1:   0.07331  417a9e5c9ac7c52e32727cfd25da99eca9339a80xor:    0.65218  119xor2:   0.29301  134217728add:    0.57841  1105

And the code used to generate this is:

 $loops = 100000; $str = "ana are mere"; echo "<pre>"; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $x = crc32($str); } $tse = microtime(true); echo "\ncrc32: \t" . round($tse-$tss, 5) . " \t" . $x; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $x = md5($str); } $tse = microtime(true); echo "\nmd5: \t".round($tse-$tss, 5) . " \t" . $x; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $x = sha1($str); } $tse = microtime(true); echo "\nsha1: \t".round($tse-$tss, 5) . " \t" . $x; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $l = strlen($str);  $x = 0x77;  for($j=0;$j<$l;$j++){   $x = $x xor ord($str[$j]);  } } $tse = microtime(true); echo "\nxor: \t".round($tse-$tss, 5) . " \t" . $x; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $l = strlen($str);  $x = 0x08;  for($j=0;$j<$l;$j++){   $x = ($x<<2) xor $str[$j];  } } $tse = microtime(true); echo "\nxor2: \t".round($tse-$tss, 5) . " \t" . $x; $tss = microtime(true); for($i=0; $i<$loops; $i++){  $l = strlen($str);  $x = 0;  for($j=0;$j<$l;$j++){   $x = $x + ord($str[$j]);  } } $tse = microtime(true); echo "\nadd: \t".round($tse-$tss, 5) . " \t" . $x;


CRC32 is pretty fast and there's a function for it: http://www.php.net/manual/en/function.crc32.php

But you should be aware that CRC32 will have more collisions than MD5 or even SHA-1 hashes, simply because of the reduced length (32 bits compared to 128 bits respectively 160 bits). But if you just want to check whether a stored string is corrupted, you'll be fine with CRC32.


Ranked list where each loop shares the same thing to crypt as all the others.

<?phpset_time_limit(720);$begin = startTime();$scores = array();foreach(hash_algos() as $algo) {    $scores[$algo] = 0;}for($i=0;$i<10000;$i++) {    $number = rand()*100000000000000;    $string = randomString(500);    foreach(hash_algos() as $algo) {        $start = startTime();        hash($algo, $number); //Number        hash($algo, $string); //String        $end = endTime($start);        $scores[$algo] += $end;    }   }asort($scores);$i=1;foreach($scores as $alg => $time) {    print $i.' - '.$alg.' '.$time.'<br />';    $i++;}echo "Entire page took ".endTime($begin).' seconds<br />';echo "<br /><br /><h2>Hashes Compared</h2>";foreach($scores as $alg => $time) {    print $i.' - '.$alg.' '.hash($alg,$string).'<br />';    $i++;}function startTime() {   $mtime = microtime();    $mtime = explode(" ",$mtime);    $mtime = $mtime[1] + $mtime[0];    return $mtime;   }function endTime($starttime) {   $mtime = microtime();    $mtime = explode(" ",$mtime);    $mtime = $mtime[1] + $mtime[0];    $endtime = $mtime;    return $totaltime = ($endtime - $starttime); }function randomString($length) {    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';    $string = '';        for ($p = 0; $p < $length; $p++) {        $string .= $characters[mt_rand(0, strlen($characters) - 1)];    }    return $string;}?>

And the output

1 - crc32b 0.1110363006592 - crc32 0.1120488643653 - md4 0.1207957267764 - md5 0.1388757228855 - sha1 0.1463687419896 - adler32 0.155013322837 - tiger192,3 0.1774470806128 - tiger160,3 0.1794981956489 - tiger128,3 0.18401288986210 - ripemd128 0.18405270576511 - ripemd256 0.18541121482812 - salsa20 0.19850015640313 - salsa10 0.20495629310614 - haval160,3 0.20609855651915 - haval256,3 0.20689177513116 - haval224,3 0.20695424079917 - ripemd160 0.20763826370218 - tiger192,4 0.20812582969719 - tiger160,4 0.20843863487220 - tiger128,4 0.20935940742521 - haval128,3 0.21025681495722 - sha256 0.21273803710923 - ripemd320 0.21538639068624 - haval192,3 0.21561098098825 - sha224 0.21832942962626 - haval192,4 0.25646471977227 - haval160,4 0.25656509399428 - haval128,4 0.25711345672629 - haval224,4 0.25892853736930 - haval256,4 0.25926208496131 - haval192,5 0.28843379020732 - haval160,5 0.29023981094433 - haval256,5 0.29172134399434 - haval224,5 0.29448413848935 - haval128,5 0.30022478103636 - sha384 0.35244989395137 - sha512 0.35460352897638 - gost 0.39237666130139 - whirlpool 0.62906765937840 - snefru256 0.82952904701241 - snefru 0.83398699760442 - md2 1.80192279816Entire page took 22.755341053 secondsHashes Compared1 - crc32b 761331d72 - crc32 7e8c6d343 - md4 1bc8785de173e77ef28a24bd525beb684 - md5 9f9cfa3b5b339773b8d6dd77bbe931dd5 - sha1 ca2bd798e47eab85655f0ce03fa46b2e6e20a31f6 - adler32 f5f2aefc7 - tiger192,3 d11b7615af06779259b29446948389c31d896dee25edfc508 - tiger160,3 d11b7615af06779259b29446948389c31d896dee9 - tiger128,3 d11b7615af06779259b29446948389c310 - ripemd128 5f221a4574a072bc71518d150ae907c811 - ripemd256 bc89cd79f4e70b73fbb4faaf47a3caf263baa07e72dd435a0f62afe840f5c71c12 - salsa20 91d9b963e172988a8fc2c5ff1a8d67073b2c5a09573cb03e901615dc1ea5162640f607e0d7134c981eedb761934cd8200fe90642a4608eacb82143e6e7b822c413 - salsa10 320b8cb8498d590ca2ec552008f1e55486116257a1e933d10d35c85a967f4a89c52158f755f775cd0b147ec64cde8934bae1e13bea81b8a4a55ac2c08efff4ce14 - haval160,3 27ad6dd290161b883e614015b574b109233c7c0e15 - haval256,3 03706dd2be7b1888bf9f3b151145b009859a720e3fe921a575e11be801c54c9a16 - haval224,3 16706dd2c77b1888c29f3b151745b009879a720e4fe921a576e11be817 - ripemd160 f419c7c997a10aaf2d83a5fa03c58350d9f9d2e418 - tiger192,4 112f486d3a9000f822c050a204d284d52473f267b1247dbd19 - tiger160,4 112f486d3a9000f822c050a204d284d52473f26720 - tiger128,4 112f486d3a9000f822c050a204d284d521 - haval128,3 9d9155d430218e4dcdde1c62962ecca322 - sha256 6027f87b4dd4c732758aa52049257f9e9db7244f78c132d36d47f9033b5c3b0923 - ripemd320 9ac00db553b51662826267daced37abfccca6433844f67d8f8cfd243cf78bbbf86839daf0961b61d24 - haval192,3 7d706dd2d37c1888eaa53b154948b009e09c720effed21a525 - sha224 b6395266d8c7e40edde77969359e6a5d725f322e2ea4bd73d3d2576826 - haval192,4 d87cd76e4c8006d401d7068dce5dec3d02dfa037d196ea1427 - haval160,4 f2ddd76e156d0cd40eec0b8d09c8f23d0f47a43728 - haval128,4 f066e6312b91e7ef69f26b2adbeba87529 - haval224,4 1b7cd76ea97c06d439d6068d7d56ec3d73dba0373895ea14e465bc0e30 - haval256,4 157cd76e8b7c06d432d6068d7556ec3d66dba0371c95ea14e165bc0ec31b9d3731 - haval192,5 05f9ea219ae1b98ba33bac6b37ccfe2f248511046c80c2f032 - haval160,5 e054ec218637bc8b4bf1b26b2fb40230e016190433 - haval256,5 48f6ea210ee1b98be835ac6b7dc4fe2f39841104a37cc2f06ceb2bf58ab4fe7834 - haval224,5 57f6ea2111e1b98bf735ac6b92c4fe2f43841104ab7cc2f076eb2bf535 - haval128,5 ccb8e0ac1fd12640ecd8976ab6402aa836 - sha384 bcf0eeaa1479bf6bef7ece0f5d7111c3aeee177aa7990926c633891464534cd8a6c69d905c36e882b3350ef40816ed0237 - sha512 8def9a1e6e31423ef73c94251d7553f6fe3ed262c44e852bdb43e3e2a2b76254b4da5ef25aefb32aae260bb386cd133045adfa2024b067c2990b60d6f014e03938 - gost ef6cb990b754b1d6a428f6bb5c113ee22cc9533558d203161441933d86e3b6f839 - whirlpool 54eb1d0667b6fdf97c01e005ac1febfacf8704da55c70f10f812b34cd9d45528b60d20f08765ced0ab3086d2bde312259aebf15d105318ae76995c4cf9a1e98140 - snefru256 20849cbeda5ddec5043c09d36b2de4ba0ea9296b6c9efaa7c7257f30f351aea441 - snefru 20849cbeda5ddec5043c09d36b2de4ba0ea9296b6c9efaa7c7257f30f351aea442 - md2 d4864c8c95786480d1cf821f690753dc