【问题标题】:hash function for 5 tuple - ipv4/65个元组的哈希函数 - ipv4/6
【发布时间】:2015-03-25 14:43:35
【问题描述】:

我正在为 ipv4/6 寻找一个高效的散列函数(它可以是 2 个独立的函数,每个家庭一个)

我遇到了这个问题:hash function for src dest ip + port

如何扩展它以支持 ipv6?

谢谢

【问题讨论】:

    标签: c networking hash hashtable


    【解决方案1】:

    对于 IPV4,我使用了以下功能:

    uint32_t *S_block = ... 
    // pointer to 1KB+ pre-defined random values.
    // It can be reference to program code segment, or to long "help" string.
    // For example, see: https://github.com/EvgenijM86/emercoin/blob/master/src/stun.cpp#L338
    
    uint32_t HashIPV4(uint32_t x) {
      x += x >> 13;
      for(int i = 0; i < 7; i++)
        x += ((x << 11) | (x >> (32 - 11))) ^ S_block[(unsigned char)x];
      x += x >> 17;
      return x;
    }
    

    对于IPV6,可以使用修改:

    uint32_t HashIPV6(uint32_t *p) {
      x = 0x55555555;
      for(int i = 0; i < 28; i++) {
        x = ((x << 11) | (x >> (32 - 11))); 
        x += p[i & 3] ^ S_block[(unsigned char)x];
      }
      x += x >> 33;
      return x;
    }
    

    【讨论】:

    • @maxihaptop - 如果您解释一下您的代码(方法),这将非常有帮助。
    • 循环移位哈希累加器,通过 Substitute Block 应用非线性函数。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2011-03-10
    • 2019-07-30
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多