home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 118 / cdrom118.iso / internet / webaroo / WebarooSetup.exe / Webaroo.msi / _A0DEB44B94924E89917E71AA90C5F226 / khashmir / khash.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-12-23  |  5.0 KB  |  151 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from sha import sha
  5. from random import randint
  6.  
  7. try:
  8.     from entropy import entropy
  9. except ImportError:
  10.     
  11.     def entropy(n):
  12.         s = ''
  13.         for i in range(n):
  14.             s += chr(randint(0, 255))
  15.         
  16.         return s
  17.  
  18.  
  19.  
  20. def intify(hstr):
  21.     '''20 bit hash, big-endian -> long python integer'''
  22.     if not len(hstr) == 20:
  23.         raise AssertionError
  24.     return long(hstr.encode('hex'), 16)
  25.  
  26.  
  27. def stringify(num):
  28.     '''long int -> 20-character string'''
  29.     str = hex(num)[2:]
  30.     if str[-1] == 'L':
  31.         str = str[:-1]
  32.     
  33.     if len(str) % 2 != 0:
  34.         str = '0' + str
  35.     
  36.     str = str.decode('hex')
  37.     return (20 - len(str)) * '\x00' + str
  38.  
  39.  
  40. def distance(a, b):
  41.     '''distance between two 160-bit hashes expressed as 20-character strings'''
  42.     return intify(a) ^ intify(b)
  43.  
  44.  
  45. def newID():
  46.     '''returns a new pseudorandom globally unique ID string'''
  47.     h = sha()
  48.     h.update(entropy(20))
  49.     return h.digest()
  50.  
  51.  
  52. def newIDInRange(min, max):
  53.     return stringify(randRange(min, max))
  54.  
  55.  
  56. def randRange(min, max):
  57.     return min + intify(newID()) % (max - min)
  58.  
  59.  
  60. def newTID():
  61.     return randRange(-2 ** 30, 2 ** 30)
  62.  
  63. import unittest
  64.  
  65. class NewID(unittest.TestCase):
  66.     
  67.     def testLength(self):
  68.         self.assertEqual(len(newID()), 20)
  69.  
  70.     
  71.     def testHundreds(self):
  72.         for x in xrange(100):
  73.             self.testLength
  74.         
  75.  
  76.  
  77.  
  78. class Intify(unittest.TestCase):
  79.     known = [
  80.         ('\x00' * 20, 0),
  81.         ('\xff' * 20, 0x2L ** 160 - 1)]
  82.     
  83.     def testKnown(self):
  84.         for str, value in self.known:
  85.             self.assertEqual(intify(str), value)
  86.         
  87.  
  88.     
  89.     def testEndianessOnce(self):
  90.         h = newID()
  91.         while h[-1] == '\xff':
  92.             h = newID()
  93.         k = h[:-1] + chr(ord(h[-1]) + 1)
  94.         self.assertEqual(intify(k) - intify(h), 1)
  95.  
  96.     
  97.     def testEndianessLots(self):
  98.         for x in xrange(100):
  99.             self.testEndianessOnce()
  100.         
  101.  
  102.  
  103.  
  104. class Disantance(unittest.TestCase):
  105.     known = [
  106.         (('\x00' * 20, '\xff' * 20), 2 ** 0xA0L - 1),
  107.         ((sha('foo').digest(), sha('foo').digest()), 0),
  108.         ((sha('bar').digest(), sha('bar').digest()), 0)]
  109.     
  110.     def testKnown(self):
  111.         for pair, dist in self.known:
  112.             self.assertEqual(distance(pair[0], pair[1]), dist)
  113.         
  114.  
  115.     
  116.     def testCommutitive(self):
  117.         for i in xrange(100):
  118.             x = newID()
  119.             y = newID()
  120.             z = newID()
  121.             self.assertEqual(distance(x, y) ^ distance(y, z), distance(x, z))
  122.         
  123.  
  124.  
  125.  
  126. class RandRange(unittest.TestCase):
  127.     
  128.     def testOnce(self):
  129.         a = intify(newID())
  130.         b = intify(newID())
  131.         if a < b:
  132.             c = randRange(a, b)
  133.             None(self.assertEqual if c <= c else c < b, 1, 'output out of range %d  %d  %d' % (b, c, a))
  134.         else:
  135.             c = randRange(b, a)
  136.             if c <= c:
  137.                 pass
  138.             elif not c < a:
  139.                 raise AssertionError, 'output out of range %d  %d  %d' % (b, c, a)
  140.  
  141.     
  142.     def testOneHundredTimes(self):
  143.         for i in xrange(100):
  144.             self.testOnce()
  145.         
  146.  
  147.  
  148. if __name__ == '__main__':
  149.     unittest.main()
  150.  
  151.