简体   繁体   English

如何从 PHP 中的字符串中获取 64 位 integer hash?

[英]How to get a 64 bit integer hash from a string in PHP?

I need 64 bit integer hashes of strings for something like a hash map.我需要 64 位 integer 字符串散列,例如 hash map。

It seems to me like there is no native PHP hash functionality that can return 64 bit integers?在我看来,好像没有可以返回 64 位整数的本机 PHP hash 功能?

I think it is possible to take the first part of a sha1 hash and convert it to an integer.我认为可以将 sha1 hash 的第一部分转换为 integer。 However that will not bring the best performance and the conversion seems to be tricky.但是,这不会带来最佳性能,而且转换似乎很棘手。

Of course it would be nice to use native PHP functions without installations.当然,不用安装就可以使用本机 PHP 功能。

I tried a lot, especially to convert a full 64 bit hex string to an signed 64 bit integer.我尝试了很多,尤其是将完整的 64 位十六进制字符串转换为有符号的 64 位 integer。 Now I ended up with this:现在我结束了:

function sha1_64bitInt($str) {
    $u = unpack('N2', sha1($str, true));
    return ($u[1] << 32) | $u[2];
}

The performance is somewhere in the middle.性能介于中间。 A lot better than implementing a full hash algorithm (like SimpleHash or dbj2) and a lot slower than a naked call to sha1() or crc32 .比实现完整的 hash 算法(如 SimpleHash 或 dbj2)要好得多,并且比对sha1()crc32的裸调用慢得多。

When there will be once a better solution to convert to a 64 bit integer it will be possible to improve this function without breaking backwards compatibility (I hope).如果有更好的解决方案可以转换为 64 位 integer,则可以在不破坏向后兼容性的情况下改进此 function(我希望)。

See this page for info on an md5 hash:有关 md5 hash 的信息,请参阅此页面:

http://us.php.net/manual/en/function.md5.php http://us.php.net/manual/en/function.md5.php

This will output a 32 char hex string.这将 output 一个 32 字符的十六进制字符串。 Each hex char represents 4 bits of data.每个十六进制字符代表 4 位数据。 this means you need 16 hex chars (or half the md5 hash) to generate 64 bits.这意味着您需要 16 个十六进制字符(或 md5 哈希的一半)来生成 64 位。

you can then use hexdec to convert the 64 bits (16x4=64) from hex to an int.然后,您可以使用 hexdec 将 64 位 (16x4=64) 从 hex 转换为 int。 Note if you pas more than 64 bits the function will overflow to the float type to try to represent the larger number请注意,如果您传递超过 64 位,则 function 将溢出到浮点类型以尝试表示更大的数字

http://php.net/hexdec http://php.net/hexdec

So basically所以基本上

$stringToHash= "abcdefghijk...";
$hash = md5($stringToHash);
$substring = substr($hash, 0,16);
$finalInt = hexdec($substring);

That should work for you.那应该对你有用。 (but i haven't tested it). (但我没有测试过)。

See http://php.net/manual/ru/function.crc32.php#111699 for CRC64 implementation.有关 CRC64 实现,请参见http://php.net/manual/ru/function.crc32.php#111699

If nobody else has a better idea, you can probably modify murmurhash_php to invoke the 64-bit version of MurmurHash3 pretty easily.如果没有其他人有更好的主意,您可能可以修改murmurhash_php以非常轻松地调用 64 位版本的MurmurHash3

PHP doesn't support 64 bits integers I think.我认为 PHP 不支持 64 位整数。 There is a BigInteger class (not native PHP) that you could use (and find if you Google really well).有一个 BigInteger class (不是原生 PHP),你可以使用(如果你用谷歌搜索的话)。 But it just uses strings internally and it's hella complex.但它只是在内部使用字符串,而且非常复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM