将IP地址散列到[0,H)的数字中 [英] Hashing an IP address to a number in [0, H)

查看:133
本文介绍了将IP地址散列到[0,H)的数字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python-2.6。我对散列函数知之甚少。



我想用一个CRC散列函数将一个IP地址如'128.0.0.5'散列到[0,H )。目前我正在考虑购买

  zlib.crc32('128.0.0.5')%H。 

这样好吗?有几个问题。你可以尝试并回答... ...


  • 它是否会产生任何差异。如果我散列'128.0.0.5'或其二进制'0001110101010 ..',不论是否有'。'


  • .zlib.crc32返回一个签名整数。修改(%)否定。有一个肯定的H总是给一个pos否?

  • %-ing by H是否会影响散列函数的良好性能? (我的意思是,我可以做的最好的可用空间,可用xlib.crc32)





解决方案


它会产生任何差异。如果我散列'128.0.0.5'或其二进制'0001110101010 ..',不论是否有'。'


真的。


zlib.crc32返回一个有符号的整数。修改(%)否定。一个积极的H总是给一个pos没有?

是。


%-ing by H是否影响散列函数的效果? (我的意思是,我可以用可用空间做的最好的,可用的xlib.crc32)

你最好使用校验和的所有部分来弥补它们缺乏雪崩效应。诸如 192.168.1.1 192.168.1.2 等一位数字的变体可能仅在前几位校验和,并且自只关心最后一位,哈希将会发生冲突。


I'm using Python-2.6. I have very little knowledge of hash functions.

I want to use a CRC hash function to hash an IP address like '128.0.0.5' into the range [0, H). Currently I'm thinking of doing

zlib.crc32('128.0.0.5')%H.

Is this okay? There's a few ques. you could try and answer...

  • does it make any diff. if I hash '128.0.0.5' or its binary '0001110101010..' whatever that is or without the '.'s

  • zlib.crc32 returns a signed integer. Does modding (%) a neg. with a positive H always give a pos no?

  • Does %-ing by H affect how good the hash function is? ( I mean is that the best I could do for the available space, with the available xlib.crc32)

Thanks!

解决方案

does it make any diff. if I hash '128.0.0.5' or its binary '0001110101010..' whatever that is or without the '.'s

Not really.

zlib.crc32 returns a signed integer. Does modding (%) a neg. with a positive H always give a pos no?

Yes.

Does %-ing by H affect how good the hash function is? ( I mean is that the best I could do for the available space, with the available xlib.crc32)

You'd better use all the bits of the checksum to make up for their lack of an "avalanche effect". Single-digit variations such as 192.168.1.1, 192.168.1.2, etc might produce differences only in the first bits of the checksum, and since % cares only about the last bits, hashes will collide.

这篇关于将IP地址散列到[0,H)的数字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆