16bit hash

Nick Craig-Wood nick at craig-wood.com
Wed Jun 27 13:30:07 EDT 2007


Robin Becker <robin at reportlab.com> wrote:
>  Is the any way to get an efficient 16bit hash in python?

Here is a 32 bit crc of which you could use the bottom 16 bits as a 16
bit hash...

  >>> import binascii
  >>> binascii.crc32("hello world")
  222957957
  >>> crc = binascii.crc32("hello")
  >>> crc = binascii.crc32(" world", crc)
  >>> crc
  222957957
  >>> 

And in case you are wondering how fast it is...

  $ python -m timeit -s 's="."*1024*1024; from binascii import crc32' 'crc32(s)'
  100 loops, best of 3: 4.25 msec per loop

Which is 235 MB/s

Ignore the bit in the binascii docs which says "Since the algorithm is
designed for use as a checksum algorithm, it is not suitable for use
as a general hash algorithm".  Actually CRCs make pretty good hash
algorithms if you want something for making a hash table.  They make
very bad cryptographic hash generators since they are linear and thus
easily forgeable.  That said you aren't going to be doing any serious
crypto with only 16 bits.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list