PDA

View Full Version : Performance comparison of hashing algorithms



Kyohack
01-21-2013, 12:04 AM
Today I was faced with the decision of which hashing algorithm to choose when verifying data integrity. For casual use, the speed of each algorithm is irrelevant. However, the hashing algorithm that I decide to implement will be heavily used several million times throughout the lifetime of my code. With the impact of server performance in mind, I wanted a hashing algorithm that could perform as fast as possible. In my tests, I used the hash_algos() function to show all algorithms supported by my installation of PHP. To test the speed of each algorithm, I ran a hash() function within a loop for 5 million times. For anyone interested in running the test on their own, here is the PHP code used:


<?php
//
// Description: Tests hashing algorithm performance within PHP.
// Programmer(s): Kyohack
// Last revision: 1/20/2013
//
// Copyright ©2007-2013 PokéCheats. All Rights Reserved.
//

set_time_limit(0);
echo '<b>HASHING ALGORITHM PERFORMANCE RESULTS:</b><br><table><tbody><tr><td>Algorithm Name:</td><td>Time Elapsed (in seconds):</td><td>Hashes Performed:</td><td>Output Hash:</td></tr>';
foreach (hash_algos() as $algorithm) {
$startTime = microtime(true);
$text = 'The quick brown fox jumped over the lazy dog.';
for ($i = 1; $i < 5000000; $i++) {
hash($algorithm, $text);
}
$totalTime = substr(microtime(true) - $startTime, 0, 6);
echo "<tr><td>$algorithm</td><td>$totalTime</td><td>$i</td><td>" . hash($algorithm, $text) . '</td></tr>';
}
echo '</tbody></table>';

?>

Among the fastest hashing algorithms are crc32, md4, md5, and sha1. The full results of the tests are listed in the table below. On the left is the name of the hashing algorithm used, and on the right is the time elapsed (in seconds), while hashing was performed 5 million times:


md2
125.48


md4
4.7988


md5
5.1507


sha1
5.7158


sha256
8.6089


sha384
27.169


sha512
27.423


ripemd128
8.6427


ripemd160
9.7001


ripemd256
8.4479


ripemd320
9.5927


whirlpool
33.518


tiger128,3
7.1539


tiger160,3
7.1137


tiger192,3
7.1175


tiger128,4
7.9517


tiger160,4
7.6474


tiger192,4
7.7109


snefru
26.519


gost
22.778


adler32
8.1629


crc32
4.5130


crc32b
4.6702


haval128,3
9.7242


haval160,3
9.9183


haval192,3
10.037


haval224,3
10.171


haval256,3
9.9377


haval128,4
11.898


haval160,4
11.977


haval192,4
12.072


haval224,4
12.212


haval256,4
12.226


haval128,5
13.296


haval160,5
13.225


haval192,5
13.390


haval224,5
13.513


haval256,5
13.622