Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing an IP address to a number in [0, H)

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!

like image 893
Lavanya Avatar asked Sep 14 '25 16:09

Lavanya


1 Answers

Why do you want to hash an IP address into a number? They already have a native integer representation. For example, using netaddr:

>>> import netaddr
>>> ip = netaddr.IPAddress('192.168.1.1')
>>> ip.value
3232235777
>>> netaddr.IPAddress(3232235777)
IPAddress('192.168.1.1')
like image 51
jathanism Avatar answered Sep 17 '25 06:09

jathanism