非加密用途的最快哈希?

Joh*_*ohn 135 php database security hash

我基本上准备将短语放入数据库,它们可能会出错,所以我想要存储它们的短哈希(我将简单地比较它们是否存在,所以哈希是理想的).

我假设MD5在100,000多个请求上相当慢,所以我想知道什么是散列短语的最佳方法,可能推出我自己的哈希函数或使用hash('md4', '...'最终会更快?

我知道MySQL有MD5(),所以这会在查询结束时补充一点速度,但也许在MySQL中还有一个更快的哈希函数,我不知道这可以用于PHP ..

Qua*_*mis 134

fcn     time  generated hash
crc32:  0.03163  798740135
md5:    0.0731   0dbab6d0c841278d33be207f14eeab8b
sha1:   0.07331  417a9e5c9ac7c52e32727cfd25da99eca9339a80
xor:    0.65218  119
xor2:   0.29301  134217728
add:    0.57841  1105
Run Code Online (Sandbox Code Playgroud)

用于生成此代码的代码是:

 $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;
Run Code Online (Sandbox Code Playgroud)

  • 啊,实际上,谢谢你的见解,只是强化了我对CRC32的使用速度最快. (3认同)
  • @Quamis测试很好但可能会产生误导 - 因为@samTolton注意到结果不同而且`md5`更快.更好的测试是将字符串内容和长度随机化.通过这种方式,我们可以更好地了解实际的现实表现.这也可以避免缓存.看看:[php哈希校验和性能](https://gist.github.com/shlomohass/1d20a8dfb00a9031a451b60e19025f73#file-php_hashing_checksum_performance-php) (3认同)
  • 只是一个简短的说明 - 我用更长的字符串(~5000 个字符)尝试了这个,CRC32 比我机器上的 MD5 和 SHA1 慢(i7-6650U,16GB)。CRC32 - 1.7s,MD5 - 1.4s,SHA1 - 1.5s。总是自己测试。 (2认同)

jos*_*chi 52

CRC32非常快,它有一个功能:http://www.php.net/manual/en/function.crc32.php

但是你应该知道CRC32将比MD5或甚至SHA-1哈希更多地发生冲突,这仅仅是因为长度减小(32位而不是128位,分别是160位).但是,如果您只是想检查存储的字符串是否已损坏,那么CRC32就可以了.

  • 如果你有更新的英特尔CPU的好处/奢侈品,有一个crc32c汇编命令......可能真的很快(虽然不是传统的crc32值).另请参见xxhash https://code.google.com/p/xxhash/ (3认同)
  • @John:不是.在ARM处理器上,CRC32比MD4低_slower_,并且速度不比MD5快.此外,CRC32使用无符号32位整数类型,这正是MD5所需要的全部...... (2认同)

Pez*_*kow 40

排名列表,其中每个循环与其他所有循环共享相同的内容.

<?php

set_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;
}

?>
Run Code Online (Sandbox Code Playgroud)

和输出

1 - crc32b 0.111036300659
2 - crc32 0.112048864365
3 - md4 0.120795726776
4 - md5 0.138875722885
5 - sha1 0.146368741989
6 - adler32 0.15501332283
7 - tiger192,3 0.177447080612
8 - tiger160,3 0.179498195648
9 - tiger128,3 0.184012889862
10 - ripemd128 0.184052705765
11 - ripemd256 0.185411214828
12 - salsa20 0.198500156403
13 - salsa10 0.204956293106
14 - haval160,3 0.206098556519
15 - haval256,3 0.206891775131
16 - haval224,3 0.206954240799
17 - ripemd160 0.207638263702
18 - tiger192,4 0.208125829697
19 - tiger160,4 0.208438634872
20 - tiger128,4 0.209359407425
21 - haval128,3 0.210256814957
22 - sha256 0.212738037109
23 - ripemd320 0.215386390686
24 - haval192,3 0.215610980988
25 - sha224 0.218329429626
26 - haval192,4 0.256464719772
27 - haval160,4 0.256565093994
28 - haval128,4 0.257113456726
29 - haval224,4 0.258928537369
30 - haval256,4 0.259262084961
31 - haval192,5 0.288433790207
32 - haval160,5 0.290239810944
33 - haval256,5 0.291721343994
34 - haval224,5 0.294484138489
35 - haval128,5 0.300224781036
36 - sha384 0.352449893951
37 - sha512 0.354603528976
38 - gost 0.392376661301
39 - whirlpool 0.629067659378
40 - snefru256 0.829529047012
41 - snefru 0.833986997604
42 - md2 1.80192279816
Entire page took 22.755341053 seconds


Hashes Compared

1 - crc32b 761331d7
2 - crc32 7e8c6d34
3 - md4 1bc8785de173e77ef28a24bd525beb68
4 - md5 9f9cfa3b5b339773b8d6dd77bbe931dd
5 - sha1 ca2bd798e47eab85655f0ce03fa46b2e6e20a31f
6 - adler32 f5f2aefc
7 - tiger192,3 d11b7615af06779259b29446948389c31d896dee25edfc50
8 - tiger160,3 d11b7615af06779259b29446948389c31d896dee
9 - tiger128,3 d11b7615af06779259b29446948389c3
10 - ripemd128 5f221a4574a072bc71518d150ae907c8
11 - ripemd256 bc89cd79f4e70b73fbb4faaf47a3caf263baa07e72dd435a0f62afe840f5c71c
12 - salsa20 91d9b963e172988a8fc2c5ff1a8d67073b2c5a09573cb03e901615dc1ea5162640f607e0d7134c981eedb761934cd8200fe90642a4608eacb82143e6e7b822c4
13 - salsa10 320b8cb8498d590ca2ec552008f1e55486116257a1e933d10d35c85a967f4a89c52158f755f775cd0b147ec64cde8934bae1e13bea81b8a4a55ac2c08efff4ce
14 - haval160,3 27ad6dd290161b883e614015b574b109233c7c0e
15 - haval256,3 03706dd2be7b1888bf9f3b151145b009859a720e3fe921a575e11be801c54c9a
16 - haval224,3 16706dd2c77b1888c29f3b151745b009879a720e4fe921a576e11be8
17 - ripemd160 f419c7c997a10aaf2d83a5fa03c58350d9f9d2e4
18 - tiger192,4 112f486d3a9000f822c050a204d284d52473f267b1247dbd
19 - tiger160,4 112f486d3a9000f822c050a204d284d52473f267
20 - tiger128,4 112f486d3a9000f822c050a204d284d5
21 - haval128,3 9d9155d430218e4dcdde1c62962ecca3
22 - sha256 6027f87b4dd4c732758aa52049257f9e9db7244f78c132d36d47f9033b5c3b09
23 - ripemd320 9ac00db553b51662826267daced37abfccca6433844f67d8f8cfd243cf78bbbf86839daf0961b61d
24 - haval192,3 7d706dd2d37c1888eaa53b154948b009e09c720effed21a5
25 - sha224 b6395266d8c7e40edde77969359e6a5d725f322e2ea4bd73d3d25768
26 - haval192,4 d87cd76e4c8006d401d7068dce5dec3d02dfa037d196ea14
27 - haval160,4 f2ddd76e156d0cd40eec0b8d09c8f23d0f47a437
28 - haval128,4 f066e6312b91e7ef69f26b2adbeba875
29 - haval224,4 1b7cd76ea97c06d439d6068d7d56ec3d73dba0373895ea14e465bc0e
30 - haval256,4 157cd76e8b7c06d432d6068d7556ec3d66dba0371c95ea14e165bc0ec31b9d37
31 - haval192,5 05f9ea219ae1b98ba33bac6b37ccfe2f248511046c80c2f0
32 - haval160,5 e054ec218637bc8b4bf1b26b2fb40230e0161904
33 - haval256,5 48f6ea210ee1b98be835ac6b7dc4fe2f39841104a37cc2f06ceb2bf58ab4fe78
34 - haval224,5 57f6ea2111e1b98bf735ac6b92c4fe2f43841104ab7cc2f076eb2bf5
35 - haval128,5 ccb8e0ac1fd12640ecd8976ab6402aa8
36 - sha384 bcf0eeaa1479bf6bef7ece0f5d7111c3aeee177aa7990926c633891464534cd8a6c69d905c36e882b3350ef40816ed02
37 - sha512 8def9a1e6e31423ef73c94251d7553f6fe3ed262c44e852bdb43e3e2a2b76254b4da5ef25aefb32aae260bb386cd133045adfa2024b067c2990b60d6f014e039
38 - gost ef6cb990b754b1d6a428f6bb5c113ee22cc9533558d203161441933d86e3b6f8
39 - whirlpool 54eb1d0667b6fdf97c01e005ac1febfacf8704da55c70f10f812b34cd9d45528b60d20f08765ced0ab3086d2bde312259aebf15d105318ae76995c4cf9a1e981
40 - snefru256 20849cbeda5ddec5043c09d36b2de4ba0ea9296b6c9efaa7c7257f30f351aea4
41 - snefru 20849cbeda5ddec5043c09d36b2de4ba0ea9296b6c9efaa7c7257f30f351aea4
42 - md2 d4864c8c95786480d1cf821f690753dc
Run Code Online (Sandbox Code Playgroud)

  • 最后有一个最小的off-by-one错误.`strlen($ characters)`应该是`strlen($ characters) - 1` :) (4认同)

hda*_*nte 26

在xxhash网站上有速度比较.复制粘贴在这里:

 Name            Speed       Q.Score   Author
 xxHash          5.4 GB/s     10
 MumurHash 3a    2.7 GB/s     10       Austin Appleby
 SpookyHash      2.0 GB/s     10       Bob Jenkins
 SBox            1.4 GB/s      9       Bret Mulvey
 Lookup3         1.2 GB/s      9       Bob Jenkins
 CityHash64      1.05 GB/s    10       Pike & Alakuijala
 FNV             0.55 GB/s     5       Fowler, Noll, Vo
 CRC32           0.43 GB/s     9
 MD5-32          0.33 GB/s    10       Ronald L. Rivest
 SHA1-32         0.28 GB/s    10
Run Code Online (Sandbox Code Playgroud)

所以看起来xxHash是迄今为止最快的一个,而其他许多都是旧的哈希,比如CRC32,MD5和SHA.

https://code.google.com/p/xxhash/

请注意,这是32位编译的排序.在64位编译中,性能顺序可能非常不同.一些散列很大程度上基于64位乘法和提取.


Aal*_*abi 17

+-------------------+---------+------+--------------+
|       NAME        |  LOOPS  | TIME |     OP/S     |
+-------------------+---------+------+--------------+
| sha1ShortString   | 1638400 | 2.85 | 574,877.19   |
| md5ShortString    | 2777680 | 4.11 | 675,834.55   |
| crc32ShortString  | 3847980 | 3.61 | 1,065,922.44 |
| sha1MediumString  | 602620  | 4.75 | 126,867.37   |
| md5MediumString   | 884860  | 4.69 | 188,669.51   |
| crc32MediumString | 819200  | 4.85 | 168,907.22   |
| sha1LongString    | 181800  | 4.95 | 36,727.27    |
| md5LongString     | 281680  | 4.93 | 57,135.90    |
| crc32LongString   | 226220  | 4.95 | 45,701.01    |
+-------------------+---------+------+--------------+
Run Code Online (Sandbox Code Playgroud)

似乎crc32对于小消息(在这种情况下为26个字符)更快,而md5对于更长消息(在这种情况下> 852个字符)更快.


Tho*_*nin 7

不要假设MD5"相当慢",试试吧.在简单的PC(我的,2.4 GHz Core2,使用单核)上简单的基于C的MD5实现可以每秒散布6 百万条小消息.这里有一条小信息,最多可达55个字节.对于较长的消息,MD5散列速度与消息大小成线性关系,即它以大约每秒400兆字节的速度处理数据.您可能会注意到这是好硬盘或千兆以太网网卡的最大速度的四倍.

由于我的PC有四个内核,这意味着哈希数据与我的硬盘一样快,可以提供或接收最多可用计算能力的6%.哈希速度成为瓶颈甚至在PC上引起显着成本需要非常特殊的情况.

在更小的架构上,散列速度可能会变得有些相关,您可能需要使用MD4.MD4适用于非加密目的(出于加密目的,您不应该使用MD5).据报道,在基于ARM的平台上,MD4甚至比CRC32更快.

  • 如果您没有使用足够宽的输出,那么您将获得随机冲突,这将是不好的,因为目标是查询数据库以了解给定的"短语"是否已知; 这里的碰撞变成了误报.使用32位,只要有60000左右的短语,就会开始看到冲突.对于所有哈希函数都是如此,加密与否.话虽这么说,您可以随时获取散列函数的输出并将其截断为您认为合适的任何长度,在上述限制范围内. (2认同)

use*_*461 7

2016年更新:事情已经发生了变化.

目前的建议应该是使用Murmur Hash Family(具体参见murmur2murmur3变体).

Murmur哈希设计用于快速散列,具有最小的冲突(比CRC,MDx和SHAx快得多).它非常适合查找重复项并且非常适合HashTable索引.

事实上,许多现代数据库(Redis,ElastisSearch,Cassandra)使用它来计算各种用途的各种哈希值.这种特定算法是当前十年中许多性能改进的根源.

它也用于Bloom Filters的实现.您应该知道,如果您正在搜索"快速哈希",那么您可能面临Bloom过滤器解决的典型问题.;-)

注意:杂音是一种通用哈希,意思是NON加密.它不会阻止查找生成哈希的源"文本".它不适合散列密码.

更多细节:MurmurHash - 它是什么?

  • 有一个公开的请求[here](https://bugs.php.net/bug.php?id=68109),可以将murmurhash添加到php,您可以对其进行投票。 (2认同)

Ana*_*ist 5

警告

下面的答案没有回答所提出的问题,因为它不推荐哈希函数。请记住,“哈希函数是可用于将任意大小的数据映射到固定大小的值的任何函数。” (维基百科)下面的答案建议进行不保证固定大小结果的转换。

如果您愿意放宽使用哈希函数的要求,请继续阅读...

原答案

我建议使用 urlencode() 或 base64_encode() ,原因如下:

  • 你不需要密码学
  • 你想要速度
  • 您需要一种方法来识别唯一字符串,同时清理“格式错误”的字符串

通过调整这些回复中其他地方的基准代码,我已经证明这两种算法都比任何哈希算法都要快。根据您的应用程序,您也许可以使用 urlencode() 或 base64_encode() 来清理要存储的任何“格式错误”字符串。


rog*_*ack 5

如果您正在寻找快速且独特的工具,我推荐 xxHash 或使用较新 cpu 的 crc32c 内置命令的工具,请参阅 /sf/answers/799573561/。如果您不太关心冲突的可能性,它还可以链接到可能更快的哈希值。