PHP integer overflow hash function

I looked at a lot of questions here in the PHP Integer Overflow section, but I can't find anything that answers my specific question, so I hope I haven't missed the existing answer.

I want to use djb2 โ€‹โ€‹hash in PHP for hash keys in something like a fragment identifier (domain index for SimpleDB). It overflows the unsigned long int, so I cannot do it the same way in direct PHP, because the native int int PHPs are 32-bit.

So, I tried PHP bc and libgmp mathematical extensions that allow arbitrary lengths and they bypass the signature / scale problem, but they make ints "too big", that is, they do not overflow.

Using GMP in particular works and seems to give consistent results, but obviously an order of magnitude slower than in C (0m0.017s vs 0m0.002s). I do not know if this is simply because it is PHP versus C, or would it be much faster in PHP if I could overwhelm it. I would rather check and find out, but I see no way to do this.

So, is there a way to force ULONG to maximum in PHP? Maybe I have to wrap the C function in a PHP extension? Or, given that I only plan to hash short characters (maybe 64 characters or less), will this provide a significant reduction in return?

+3
source share
1 answer

, - 32 ? long , >= 32 . 32- long 32 .

, , , , 64 , , , long long ( >= 64 32- ), , .

"djb2" - -, , . modulo , "modulo 2^(sizeof long)". ( ) , C, , , -. PHP, double .

PHP, , , PHP_INT_MAX ( 64- PHP, ?). , (33) , , .

0

Source: https://habr.com/ru/post/1783926/


All Articles