Get 32-bit hash value from boost::hash

后端 未结 3 1816
感动是毒
感动是毒 2021-02-16 00:29

I am using boost::hash to get hash value for a string. But it is giving different hash values for same string on Windows 32-bit and Debian 64-bit systems.

S

3条回答
  •  庸人自扰
    2021-02-16 01:05

    Hash-function above is simple, but weak and vulnerable.

    For example, pass to that function string like "bb" "bbbb" "bbddbb" "ddffbb" -- any combination of pairs symbols with even ASCII codes, and watch for low byte. It always will be 57.

    Rather, I recommend to use my hash function, which is relative lightweight, and does not have easy vulnerabilities:

    #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);
    }
    

提交回复
热议问题