PHP中的hash函数各种algo的执行效率和结果比较示例
测试代码:
1 public function test(Request $request) 2 { 3 $algos = hash_algos(); 4 $plain = (string)time(); 5 $result = []; 6 foreach ($algos as $algo) { 7 // 这是一个取当前微秒时间戳的方法,返回float 8 $start = \app\util\Utils::microtimeFloat(); 9 for ($i = 0; $i < 500000; $i++) { 10 $hash = hash($algo, $plain); 11 } 12 $end = \app\util\Utils::microtimeFloat(); 13 $duration = $end - $start; 14 $result[] = [ 15 'algo' => $algo, 16 'plain' => $plain, 17 'hash' => $hash, 18 'hash_length' => mb_strlen($hash), 19 'duration' => $duration, 20 ]; 21 } 22 usort($result, function ($a, $b) { 23 if ($a['duration'] == $b['duration']) return 0; 24 return ($a['duration'] < $b['duration']) ? -1 : 1; 25 }); 26 return json($result); 27 }
简单说明:
第3行hash_algo()函数返回由当前系统所有已注册的哈希算法组成的数组,不同的环境下hash_algo()函数的返回值可能不相同;
第9行可以调整循环次数来调整程序执行时间;
第10行就是具体hash()函数的用法。
补充一点,每次的执行结果(排序)不完全相同,但是变化不会太大。
执行结果示例:
[ { "algo": "fnv1a32", "plain": "1659420397", "hash": "4e10797f", "hash_length": 8, "duration": 0.07148289680480957 }, { "algo": "fnv132", "plain": "1659420397", "hash": "be532d9b", "hash_length": 8, "duration": 0.07168698310852051 }, { "algo": "adler32", "plain": "1659420397", "hash": "0b45020f", "hash_length": 8, "duration": 0.0740518569946289 }, { "algo": "fnv164", "plain": "1659420397", "hash": "29fa23d647cfc59b", "hash_length": 16, "duration": 0.07604503631591797 }, { "algo": "joaat", "plain": "1659420397", "hash": "d4a29194", "hash_length": 8, "duration": 0.07701706886291504 }, { "algo": "crc32b", "plain": "1659420397", "hash": "ed73a030", "hash_length": 8, "duration": 0.0777900218963623 }, { "algo": "fnv1a64", "plain": "1659420397", "hash": "41a1c02b63350eff", "hash_length": 16, "duration": 0.07889008522033691 }, { "algo": "crc32c", "plain": "1659420397", "hash": "0d7f8d9a", "hash_length": 8, "duration": 0.07942700386047363 }, { "algo": "crc32", "plain": "1659420397", "hash": "dc146b78", "hash_length": 8, "duration": 0.08387088775634766 }, { "algo": "md4", "plain": "1659420397", "hash": "568f04b82ecdfb2dfced45c8b9e44ff7", "hash_length": 32, "duration": 0.1749119758605957 }, { "algo": "md5", "plain": "1659420397", "hash": "b978bbd9beab5589e7600d833943e927", "hash_length": 32, "duration": 0.1823258399963379 }, { "algo": "tiger128,3", "plain": "1659420397", "hash": "591bc2a43ac5f1c6e67488df6ac3bba9", "hash_length": 32, "duration": 0.221268892288208 }, { "algo": "tiger160,3", "plain": "1659420397", "hash": "591bc2a43ac5f1c6e67488df6ac3bba9ec0bdc85", "hash_length": 40, "duration": 0.22714686393737793 }, { "algo": "tiger192,3", "plain": "1659420397", "hash": "591bc2a43ac5f1c6e67488df6ac3bba9ec0bdc85bd32f707", "hash_length": 48, "duration": 0.23341989517211914 }, { "algo": "sha1", "plain": "1659420397", "hash": "8e8005af257645a0687619531b0a5dbdd88cb80f", "hash_length": 40, "duration": 0.24950408935546875 }, { "algo": "tiger128,4", "plain": "1659420397", "hash": "f4ce49c0e504de02f31888aa2524994b", "hash_length": 32, "duration": 0.2621622085571289 }, { "algo": "tiger160,4", "plain": "1659420397", "hash": "f4ce49c0e504de02f31888aa2524994b631f6071", "hash_length": 40, "duration": 0.265643835067749 }, { "algo": "tiger192,4", "plain": "1659420397", "hash": "f4ce49c0e504de02f31888aa2524994b631f60717d29a9c9", "hash_length": 48, "duration": 0.2685270309448242 }, { "algo": "ripemd128", "plain": "1659420397", "hash": "389770561bbce2c3df6d018db5654c4a", "hash_length": 32, "duration": 0.3326890468597412 }, { "algo": "ripemd256", "plain": "1659420397", "hash": "742166226d69f87397940274eb28991db2a756008bed0e5d7f99f3e80ea871ff", "hash_length": 64, "duration": 0.35540294647216797 }, { "algo": "ripemd160", "plain": "1659420397", "hash": "49cf7ce5e3d7c5a9dba7a5e4563fae4563473a8b", "hash_length": 40, "duration": 0.4622070789337158 }, { "algo": "ripemd320", "plain": "1659420397", "hash": "c481139a35e6854ab07304fbdb623d7c2b34ceaa6b8622c98e9811928440e437a8cc2e0ddf64a705", "hash_length": 80, "duration": 0.46743297576904297 }, { "algo": "sha224", "plain": "1659420397", "hash": "ba69be50571bb18f4604eb43176d7350213611dff5fa87f25594402b", "hash_length": 56, "duration": 0.5250968933105469 }, { "algo": "sha256", "plain": "1659420397", "hash": "f9354fe440ae16e33058f0f059f135c49026f7a7cb85acad2e14894b645f8777", "hash_length": 64, "duration": 0.5259490013122559 }, { "algo": "sha3-256", "plain": "1659420397", "hash": "8287430a8fdff8d45dde03c3f234b496148fa5ac1cde6e44886d550d1c43c7cc", "hash_length": 64, "duration": 0.5908019542694092 }, { "algo": "sha3-224", "plain": "1659420397", "hash": "7ed1cba525e55172f33d7a0dce96e3705772938863523842562f7566", "hash_length": 56, "duration": 0.6054599285125732 }, { "algo": "sha3-384", "plain": "1659420397", "hash": "32729890564d23496fa89c25f26df104fc024cd52add8dfa5c59df2b6b51ce20e89a43ef6e86565eb68eebfa06738eab", "hash_length": 96, "duration": 0.6107051372528076 }, { "algo": "haval160,3", "plain": "1659420397", "hash": "aeb7ad9b1fb1c821651e46f1d14fb9cb863132ca", "hash_length": 40, "duration": 0.6178841590881348 }, { "algo": "haval128,3", "plain": "1659420397", "hash": "7f063635ce6a9f7329625097057c4475", "hash_length": 32, "duration": 0.6194069385528564 }, { "algo": "haval256,3", "plain": "1659420397", "hash": "ea3a86185aee5834d126ffea06c178127b30cb8114f60d824f49a864523f38d5", "hash_length": 64, "duration": 0.6264939308166504 }, { "algo": "haval192,3", "plain": "1659420397", "hash": "15ad75fe42020eb8ee33e855596fb56a84467dabd56918ab", "hash_length": 48, "duration": 0.627838134765625 }, { "algo": "haval224,3", "plain": "1659420397", "hash": "ca0a3bf911645793de300abbc4ec99e45e397c2f74420b3da022ec75", "hash_length": 56, "duration": 0.6293528079986572 }, { "algo": "sha3-512", "plain": "1659420397", "hash": "25dfb1e55d1da549fecde8aa67b0f84ca9edb8b9f7addbb41107348c5a12abde746d8fb26f503ca04761a39b3ffdc92c53113338f6cff62e47b9a73d4f6c2ff0", "hash_length": 128, "duration": 0.6317479610443115 }, { "algo": "whirlpool", "plain": "1659420397", "hash": "414bc2d035a125f6f97b51bbf2deba10f54f922c60af8bd01c4bd5ce8e7749aa008eb3c0ca16d7db22ab9fef73008989a99a11ea603bf236f1be1ed1bcb0122d", "hash_length": 128, "duration": 0.6433639526367188 }, { "algo": "sha512/224", "plain": "1659420397", "hash": "f9b6efe977f3fdea47f321b8258b2e5cb23a82f218f064a792fb7664", "hash_length": 56, "duration": 0.6592311859130859 }, { "algo": "sha512/256", "plain": "1659420397", "hash": "f7ac02935bb4496c6df6766ed12bad5c9ded41933f175f4ebe2b73f5f00ec7ad", "hash_length": 64, "duration": 0.6618289947509766 }, { "algo": "sha384", "plain": "1659420397", "hash": "c8fc41acd4a6f02593ffe7eb09df15e1db6b35875d0c3b3bd7ed563251aee0f01388cb0c6ff87efddd333b09cdab0b6e", "hash_length": 96, "duration": 0.6621658802032471 }, { "algo": "sha512", "plain": "1659420397", "hash": "7ea4c79d863a9b88a184f0aad0a380e6c499dc4268805603c0063e1e129a2458f70eae2a3034b59b911a499d428d03da15728d24ce768795da59339100ebdd77", "hash_length": 128, "duration": 0.6865909099578857 }, { "algo": "haval128,4", "plain": "1659420397", "hash": "64b2db22c5413f815a3aba67721d8159", "hash_length": 32, "duration": 0.8063299655914307 }, { "algo": "haval256,4", "plain": "1659420397", "hash": "a22a11d6dba7417a5a8cca0d2f34a2b606dea83bc7be453f2afe4890f2c9aacb", "hash_length": 64, "duration": 0.8122670650482178 }, { "algo": "haval192,4", "plain": "1659420397", "hash": "f1ca06b62e01562a15413b3df63ce9daf66be19f3a739326", "hash_length": 48, "duration": 0.8150920867919922 }, { "algo": "haval224,4", "plain": "1659420397", "hash": "fd679ba551347bc6390081dda27dca8b57c8a8273cb0d31127a2ad9c", "hash_length": 56, "duration": 0.8191618919372559 }, { "algo": "haval160,4", "plain": "1659420397", "hash": "7181c9a2a647d8243b4f29f9f0467082ac9dc415", "hash_length": 40, "duration": 0.8196818828582764 }, { "algo": "haval128,5", "plain": "1659420397", "hash": "795668614a77107a24b8ec0cdbbba8ba", "hash_length": 32, "duration": 0.9487621784210205 }, { "algo": "haval160,5", "plain": "1659420397", "hash": "e07e151c7f37b24c558808bca6f21615a121b102", "hash_length": 40, "duration": 0.9599108695983887 }, { "algo": "haval256,5", "plain": "1659420397", "hash": "6c6445a3fc25d0078202f8043070f204b68d31ee3a7be3f085e59b81110e9850", "hash_length": 64, "duration": 0.9725661277770996 }, { "algo": "haval192,5", "plain": "1659420397", "hash": "c2b651da40372e5b06d2b76c81a047e27d5355cd4b0614b4", "hash_length": 48, "duration": 0.9767270088195801 }, { "algo": "haval224,5", "plain": "1659420397", "hash": "bdbfbaa9ee03f877eb392e18d8986221d94e426f9fb30eff31e4a42c", "hash_length": 56, "duration": 0.9787540435791016 }, { "algo": "gost", "plain": "1659420397", "hash": "dd7d57a825bf4e570ba16ca7b375412272d5574bea58668dc818dd01f7160365", "hash_length": 64, "duration": 1.5994179248809814 }, { "algo": "gost-crypto", "plain": "1659420397", "hash": "8e8c21d7bb4117df499144aa81d38cf3514d6b73e2e5f3ed035356ff3edd405f", "hash_length": 64, "duration": 1.609407901763916 }, { "algo": "snefru", "plain": "1659420397", "hash": "4dafcc7a2b18a952347098717f64f03f8cc858c949fb4117e3efe20ad5aa69e0", "hash_length": 64, "duration": 2.076050043106079 }, { "algo": "snefru256", "plain": "1659420397", "hash": "4dafcc7a2b18a952347098717f64f03f8cc858c949fb4117e3efe20ad5aa69e0", "hash_length": 64, "duration": 2.1128909587860107 }, { "algo": "md2", "plain": "1659420397", "hash": "904027aa3abfbfd938c0f97e44787c15", "hash_length": 32, "duration": 3.920799970626831 } ]