PHP实现的各类hash算法长度及性能测试实例

秋天,稻田里的稻谷已经成熟了,一眼看去,好像铺了一地的金子,而农民伯伯们一个接一个到自己的田里捡金子。微风吹过,金色的海洋掀起一层层麦浪。

本文实例讲述了PHP实现的各类hash算法长度及性能测试。分享给大家供大家参考,具体如下:

Hash结果如下

<?php
$data = "hello world";
foreach (hash_algos() as $v) {
    $r = hash($v, $data, false);
    printf("%-12s %3d %s\n", $v, strlen($r), $r);
}
?>

运行结果:

md2      32 d9cce882ee690a5c1ce70beff3a78c77
md4      32 aa010fbc1d14c795d86ef98c95479d17
md5      32 5eb63bbbe01eeed093cb22bb8f5acdc3
sha1     40 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
sha224    56 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
sha256    64 b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
sha384    96 fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
sha512    128 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
ripemd128   32 c52ac4d06245286b33953957be6c6f81
ripemd160   40 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f
ripemd256   64 0d375cf9d9ee95a3bb15f757c81e93bb0ad963edf69dc4d12264031814608e37
ripemd320   80 0e12fe7d075f8e319e07c106917eddb0135e9a10aefb50a8a07ccb0582ff1fa27b95ed5af57fd5c6
whirlpool  128 8d8309ca6af848095bcabaf9a53b1b6ce7f594c1434fd6e5177e7e5c20e76cd30936d8606e7f36acbef8978fea008e6400a975d51abe6ba4923178c7cf90c802
tiger128,3  32 4c8fbddae0b6f25832af45e7c62811bb
tiger160,3  40 4c8fbddae0b6f25832af45e7c62811bb64ec3e43
tiger192,3  48 4c8fbddae0b6f25832af45e7c62811bb64ec3e43691e9cc3
tiger128,4  32 24465a3f6e4aa92d903ee535476591e9
tiger160,4  40 24465a3f6e4aa92d903ee535476591e937f3a14d
tiger192,4  48 24465a3f6e4aa92d903ee535476591e937f3a14d81c4c7b6
snefru    64 902b49fa8b0828b44d8ac069111899bbfaf51d334485e4b28e90c93f63bb86dd
snefru256   64 902b49fa8b0828b44d8ac069111899bbfaf51d334485e4b28e90c93f63bb86dd
gost     64 1bb6ce69d2e895a78489c87a0712a2f40258d1fae3a4666c23f8f487bef0e22a
gost-crypto  64 c5aa1455afe9f0c440eec3c96ccccb5c8495097572cc0f625278bd0da5ea5e07
adler32    8 1a0b045d
crc32     8 7813f744
crc32b     8 0d4a1185
fnv132     8 548da96f
fnv1a32    8 d58b3fa7
fnv164    16 7dcf62cdb1910e6f
fnv1a64    16 779a65e7023cd2e7
joaat     8 3e4a5a57
haval128,3  32 906c1df7cbe6d318f36ab172f95e89c0
haval160,3  40 6e733b21876e47c2168a122e23d86bdd69e50f95
haval192,3  48 ec67a6a417953fdbf3496502004b6c21b270d5890dedd931
haval224,3  56 766879d9ba1dc9e24a6040908a7ae813a47b08af5c5f3beebcacda48
haval256,3  64 45492c6c8adab277759f4381420799431a037daf6d829b8b5c21104c10f61a92
haval128,4  32 c97d46956b8e3e60acd2bb090c482c5e
haval160,4  40 2cb8b12eb5a2561022010c2a2af8795e602fdef2
haval192,4  48 39a281e4e492533b6dfea0af294149ccac771ab87204c9ec
haval224,4  56 3d64d34aea48f5e649ed6147da5d29d31c762a937e9e21f4da1f3106
haval256,4  64 0359a526d77e271707c44d9b270e68a394f8486a459f0137ad5e1d02e44c5889
haval128,5  32 8332ad9f32e385d9acd421b63ee04cfc
haval160,5  40 d33cf9052d55da9b0f506cb8849097939363e361
haval192,5  48 67c3492878c8fc4819c8589231fcfe69b15b015c1ca48ac5
haval224,5  56 6bedeb6a8676e46413c020c8813c022486ca93353b8a0673fb577ba1
haval256,5  64 f5f6ffcfe39a65ac2c3989430340420341762a6624ebd69b9d08ec1dc4b9f167

性能测试如下:

<?php
define('testtime', 1000000);
$algos = hash_algos();
foreach($algos as $algo) {
  $st = microtime();
  for($i = 0; $i < testtime; $i++) {
    hash($algo, microtime().$i);
  }
  $et = microtime();
  list($ss, $si) = explode(' ', $st);
  list($es, $ei) = explode(' ', $et);
  $time[$algo] = $ei + $es - $si - $ss;
}
asort($time, SORT_NUMERIC);
print_r($time);
?>

运行结果:

Array
(
  [fnv1a32] => 1.4528379546356
  [fnv164] => 1.4598390410767
  [fnv1a64] => 1.4685498960724
  [fnv132] => 1.4695508840027
  [crc32b] => 1.480463955719
  [adler32] => 1.481206043457
  [joaat] => 1.4851269485474
  [crc32] => 1.508364085907
  [md4] => 1.6973789288788
  [md5] => 1.7637529927979
  [sha1] => 1.932477017334
  [tiger128,3] => 1.9683119142761
  [tiger160,3] => 1.9759550503387
  [ripemd128] => 2.0003409449921
  [tiger192,3] => 2.0107291056519
  [tiger128,4] => 2.0609339611664
  [tiger160,4] => 2.0614830404358
  [ripemd256] => 2.1055679496613
  [tiger192,4] => 2.1089930283813
  [ripemd320] => 2.3564790057831
  [ripemd160] => 2.3820580299072
  [sha256] => 2.3944439311981
  [sha224] => 2.4205659084473
  [haval128,3] => 2.5319820201874
  [haval224,3] => 2.5319839861755
  [haval160,3] => 2.5347460784149
  [haval192,3] => 2.5500600071869
  [haval256,3] => 2.580485933548
  [sha384] => 2.6736448852386
  [sha512] => 2.721533025589
  [haval224,4] => 2.9019400155029
  [haval128,4] => 2.9155439011078
  [haval256,4] => 2.9168769813385
  [haval160,4] => 2.9341630749512
  [haval192,4] => 2.9478991126251
  [haval192,5] => 3.1933639251862
  [haval256,5] => 3.1942859609985
  [haval160,5] => 3.1946619862823
  [whirlpool] => 3.1954451004639
  [haval128,5] => 3.2122300287323
  [haval224,5] => 3.295264964798
  [gost] => 4.6756690344391
  [gost-crypto] => 4.6819899302826
  [snefru] => 6.5784390528107
  [snefru256] => 6.6484970919647
  [md2] => 11.291653894501
)

PS:这里再为大家提供2款hash相关在线工具供大家参考使用:

在线散列/哈希算法加密工具:
http://tools.haodaima.com/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.haodaima.com/password/hash_md5_sha

希望本文所述对大家PHP程序设计有所帮助。

本文PHP实现的各类hash算法长度及性能测试实例到此结束。失去灯火之后不必慌乱,你还可以看见满天的繁星。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
javascript for循环性能测试示例

php curl中gzip的压缩性能测试实例分析

PHP性能优化案例分享

PHP实现短信验证码的发送次数限制

PHP中的异常处理机制深入讲解