]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - hash.py
*** empty log message ***
[quix0rs-apt-p2p.git] / hash.py
1 ## Copyright 2002 Andrew Loewenstern, All Rights Reserved
2
3 from sha import sha
4 import whrandom
5
6
7 def intify(hstr):
8     """20 bit hash, big-endian -> long python integer"""
9     assert len(hstr) == 20
10     return long(hstr.encode('hex'), 16)
11
12 def stringify(num):
13     """long int -> 20-character string"""
14     str = hex(num)[2:]
15     if str[-1] == 'L':
16         str = str[:-1]
17     if len(str) % 2 != 0:
18         str = '0' + str
19     str = str.decode('hex')
20     return (20 - len(str)) *'\x00' + str
21     
22 def distance(a, b):
23     """distance between two 160-bit hashes expressed as 20-character strings"""
24     return intify(a) ^ intify(b)
25
26
27 def newID():
28     """returns a new pseudorandom globally unique ID string"""
29     h = sha()
30     for i in range(20):
31         h.update(chr(whrandom.randint(0,255)))
32     return h.digest()
33
34 def newIDInRange(min, max):
35     return stringify(randRange(min,max))
36     
37 def randRange(min, max):
38     return min + intify(newID()) % (max - min)
39     
40
41 ### Test Cases ###
42 import unittest
43
44 class NewID(unittest.TestCase):
45     def testLength(self):
46         self.assertEqual(len(newID()), 20)
47     def testHundreds(self):
48         for x in xrange(100):
49             self.testLength
50
51 class Intify(unittest.TestCase):
52     known = [('\0' * 20, 0),
53             ('\xff' * 20, 2L**160 - 1),
54             ]
55     def testKnown(self):
56         for str, value in self.known: 
57             self.assertEqual(intify(str),  value)
58     def testEndianessOnce(self):
59         h = newID()
60         while h[-1] == '\xff':
61             h = newID()
62         k = h[:-1] + chr(ord(h[-1]) + 1)
63         self.assertEqual(intify(k) - intify(h), 1)
64     def testEndianessLots(self):
65         for x in xrange(100):
66             self.testEndianessOnce()
67
68 class Disantance(unittest.TestCase):
69     known = [
70             (("\0" * 20, "\xff" * 20), 2**160L -1),
71             ((sha("foo").digest(), sha("foo").digest()), 0),
72             ((sha("bar").digest(), sha("bar").digest()), 0)
73             ]
74     def testKnown(self):
75         for pair, dist in self.known:
76             self.assertEqual(distance(pair[0], pair[1]), dist)
77     def testCommutitive(self):
78         for i in xrange(100):
79             x, y, z = newID(), newID(), newID()
80             self.assertEqual(distance(x,y) ^ distance(y, z), distance(x, z))
81         
82 class RandRange(unittest.TestCase):
83     def testOnce(self):
84         a = intify(newID())
85         b = intify(newID())
86         if a < b:
87             c = randRange(a, b)
88             self.assertEqual(a <= c < b, 1, "output out of range %d  %d  %d" % (b, c, a))
89         else:
90             c = randRange(b, a)
91             assert b <= c < a, "output out of range %d  %d  %d" % (b, c, a)
92
93     def testOneHundredTimes(self):
94         for i in xrange(100):
95             self.testOnce()
96
97
98
99 if __name__ == '__main__':
100     unittest.main()   
101
102