Fix for hash table collision attack. We simply randomize hash table initialization value at startup time.

This commit is contained in:
antirez
2012-01-21 23:05:32 +01:00
parent 447ebf3bc7
commit a48c8d873b
4 changed files with 22 additions and 4 deletions

View File

@ -85,10 +85,20 @@ unsigned int dictIdentityHashFunction(unsigned int key)
return key;
}
static int dict_hash_function_seed = 5183;
void dictSetHashFunctionSeed(unsigned int seed) {
dict_hash_function_seed = seed;
}
unsigned int dictGetHashFunctionSeed(void) {
return dict_hash_function_seed;
}
/* Generic hash function (a popular one from Bernstein).
* I tested a few and this was the best. */
unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
unsigned int hash = 5381;
unsigned int hash = dict_hash_function_seed;
while (len--)
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
@ -97,7 +107,7 @@ unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
/* And a case insensitive version */
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
unsigned int hash = 5381;
unsigned int hash = dict_hash_function_seed;
while (len--)
hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */