The hash function above is simple, but weak and vulnerable.
For example, go to this function line, such as "bb" "bbbb", "bbddbb" "ddffbb" - any combination of character pairs with even ASCII codes and look at the low byte. It will always be 57.
Rather, I recommend using my hash function, which is relatively lightweight and has no easy vulnerability:
#define NLF(h, c) (rand[(uint8_t)(c ^ h)]) uint32_t rand[0x100] = { 256 random non-equal values }; uint32_t oleg_h(const char *key) { uint32_t h = 0x1F351F35; char c; while(c = *key++) h = ((h >> 11) | (h << (32 - 11))) + NLF(h, c); h ^= h >> 16; return h ^ (h >> 8); }
source share