Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ended up implementing it on my own. Released according to the original copyright notice (see below). Use at your own risk and have fun :-)</p> <pre><code>'''Implements a straight Jenkins lookup hash - http://burtleburtle.net/bob/hash/doobs.html Usage: from jhash import jhash print jhash('My hovercraft is full of eels') Returns: unsigned 32 bit integer value Prereqs: None Tested with Python 2.6. Version 1.00 - der@dod.no - 23.08.2010 Partly based on the Perl module Digest::JHash http://search.cpan.org/~shlomif/Digest-JHash-0.06/lib/Digest/JHash.pm Original copyright notice: By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial. It's free. See http://burtleburtle.net/bob/hash/evahash.html Use for hash table lookup, or anything where one collision in 2^^32 is acceptable. Do NOT use for cryptographic purposes. ''' def mix(a, b, c): '''mix() -- mix 3 32-bit values reversibly. For every delta with one or two bits set, and the deltas of all three high bits or all three low bits, whether the original value of a,b,c is almost all zero or is uniformly distributed, * If mix() is run forward or backward, at least 32 bits in a,b,c have at least 1/4 probability of changing. * If mix() is run forward, every bit of c will change between 1/3 and 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)''' # Need to constrain U32 to only 32 bits using the &amp; 0xffffffff # since Python has no native notion of integers limited to 32 bit # http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex a &amp;= 0xffffffff; b &amp;= 0xffffffff; c &amp;= 0xffffffff a -= b; a -= c; a ^= (c&gt;&gt;13); a &amp;= 0xffffffff b -= c; b -= a; b ^= (a&lt;&lt;8); b &amp;= 0xffffffff c -= a; c -= b; c ^= (b&gt;&gt;13); c &amp;= 0xffffffff a -= b; a -= c; a ^= (c&gt;&gt;12); a &amp;= 0xffffffff b -= c; b -= a; b ^= (a&lt;&lt;16); b &amp;= 0xffffffff c -= a; c -= b; c ^= (b&gt;&gt;5); c &amp;= 0xffffffff a -= b; a -= c; a ^= (c&gt;&gt;3); a &amp;= 0xffffffff b -= c; b -= a; b ^= (a&lt;&lt;10); b &amp;= 0xffffffff c -= a; c -= b; c ^= (b&gt;&gt;15); c &amp;= 0xffffffff return a, b, c def jhash(data, initval = 0): '''hash() -- hash a variable-length key into a 32-bit value data : the key (the unaligned variable-length array of bytes) initval : can be any 4-byte value, defaults to 0 Returns a 32-bit value. Every bit of the key affects every bit of the return value. Every 1-bit and 2-bit delta achieves avalanche.''' length = lenpos = len(data) # empty string returns 0 if length == 0: return 0 # Set up the internal state a = b = 0x9e3779b9 # the golden ratio; an arbitrary value c = initval # the previous hash value p = 0 # string offset # ------------------------- handle most of the key in 12 byte chunks while lenpos &gt;= 12: a += (ord(data[p+0]) + (ord(data[p+1])&lt;&lt;8) + (ord(data[p+2])&lt;&lt;16) + (ord(data[p+3])&lt;&lt;24)) b += (ord(data[p+4]) + (ord(data[p+5])&lt;&lt;8) + (ord(data[p+6])&lt;&lt;16) + (ord(data[p+7])&lt;&lt;24)) c += (ord(data[p+8]) + (ord(data[p+9])&lt;&lt;8) + (ord(data[p+10])&lt;&lt;16) + (ord(data[p+11])&lt;&lt;24)) a, b, c = mix(a, b, c) p += 12 lenpos -= 12 # ------------------------- handle the last 11 bytes c += length if lenpos &gt;= 11: c += ord(data[p+10])&lt;&lt;24 if lenpos &gt;= 10: c += ord(data[p+9])&lt;&lt;16 if lenpos &gt;= 9: c += ord(data[p+8])&lt;&lt;8 # the first byte of c is reserved for the length if lenpos &gt;= 8: b += ord(data[p+7])&lt;&lt;24 if lenpos &gt;= 7: b += ord(data[p+6])&lt;&lt;16 if lenpos &gt;= 6: b += ord(data[p+5])&lt;&lt;8 if lenpos &gt;= 5: b += ord(data[p+4]) if lenpos &gt;= 4: a += ord(data[p+3])&lt;&lt;24 if lenpos &gt;= 3: a += ord(data[p+2])&lt;&lt;16 if lenpos &gt;= 2: a += ord(data[p+1])&lt;&lt;8 if lenpos &gt;= 1: a += ord(data[p+0]) a, b, c = mix(a, b, c) # ------------------------- report the result return c if __name__ == "__main__": hashstr = 'My hovercraft is full of eels' myhash = jhash(hashstr) print 'jhash("%s"): %d' % (hashstr, myhash) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload