Djb2 hash function

I am using djb2 algorithm to generate a hash key for a string that looks like this

hash(unsigned char *str) { unsigned long hash = 5381; int c; while (c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ return hash; } 

Now with each cycle there is a multiplication with two large numbers. After some time, an overflow occurs with the 4th fifth character of the string when the hash value becomes huge.

What is the correct way to refactor so that the hash value does not overflow, and the hashing also happens correctly.

+6
hash string-hashing
source share
4 answers

Hash calculations are often crowded. This is not a problem at all if you have a guarantee of what will happen when it overflows. Do not forget that the hash point should not have a number that means something in terms of magic, etc. is just a way of discovering equality. Why does overflow prevent this?

+17
source share

I think you are using a static / time analyzer to warn about integer overflow? Well, this is one of those cases where you can ignore the warning. Hash functions are for certain types of properties, so don't worry about your analyzer warnings. Just don't try to create a hash function yourself.

+4
source share

You must not do this. Since there is no module, integer overflow is the expected behavior for the function (and it was designed with this in mind). Why do you want to change it?

+3
source share

return (hash and 0xFFFFFFFF); // or any other mask that you need, it does not matter as long as you save it consistently.

0
source share

All Articles