From c103b388657a5519dc1dad8769f5cd84d9748c04 Mon Sep 17 00:00:00 2001 From: aaronsw Date: Thu, 28 Nov 2002 17:58:30 +0000 Subject: [PATCH] general cleanup: tabs->spaces, comments->docstrings, etc. should whrandom become random? --- hash.py | 90 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/hash.py b/hash.py index 9ab4e0b..b6e1f29 100644 --- a/hash.py +++ b/hash.py @@ -3,31 +3,31 @@ from sha import sha import whrandom -## takes a 20 bit hash, big-endian, and returns it expressed a long python integer def intify(hstr): - assert(len(hstr) == 20) - return eval('0x' + hstr.encode('hex') + 'L') + """20 bit hash, big-endian -> long python integer""" + assert len(hstr) == 20 + return long(hstr.encode('hex'), 16)) -## takes a long int and returns a 20-character string def stringify(int): + """long int -> 20-character string""" str = hex(int)[2:] if str[-1] == 'L': - str = str[:-1] + str = str[:-1] if len(str) % 2 != 0: - str = '0' + str + str = '0' + str str = str.decode('hex') return (20 - len(str)) *'\x00' + str -## returns the distance between two 160-bit hashes expressed as 20-character strings def distance(a, b): + """distance between two 160-bit hashes expressed as 20-character strings""" return intify(a) ^ intify(b) -## returns a new pseudorandom globally unique ID string def newID(): + """returns a new pseudorandom globally unique ID string""" h = sha() for i in range(20): - h.update(chr(whrandom.randrange(0,256))) + h.update(chr(whrandom.randrange(0,256))) return h.digest() def newIDInRange(min, max): @@ -36,64 +36,66 @@ def newIDInRange(min, max): def randRange(min, max): return min + intify(newID()) % (max - min) + +### Test Cases ### import unittest class NewID(unittest.TestCase): def testLength(self): - self.assertEqual(len(newID()), 20) + self.assertEqual(len(newID()), 20) def testHundreds(self): - for x in xrange(100): - self.testLength + for x in xrange(100): + self.testLength class Intify(unittest.TestCase): known = [('\0' * 20, 0), - ('\xff' * 20, 2L**160 - 1), - ] + ('\xff' * 20, 2L**160 - 1), + ] def testKnown(self): - for str, value in self.known: - self.assertEqual(intify(str), value) + for str, value in self.known: + self.assertEqual(intify(str), value) def testEndianessOnce(self): - h = newID() - while h[-1] == '\xff': - h = newID() - k = h[:-1] + chr(ord(h[-1]) + 1) - self.assertEqual(intify(k) - intify(h), 1) + h = newID() + while h[-1] == '\xff': + h = newID() + k = h[:-1] + chr(ord(h[-1]) + 1) + self.assertEqual(intify(k) - intify(h), 1) def testEndianessLots(self): - for x in xrange(100): - self.testEndianessOnce() + for x in xrange(100): + self.testEndianessOnce() class Disantance(unittest.TestCase): known = [ - (("\0" * 20, "\xff" * 20), 2**160L -1), - ((sha("foo").digest(), sha("foo").digest()), 0), - ((sha("bar").digest(), sha("bar").digest()), 0) - ] + (("\0" * 20, "\xff" * 20), 2**160L -1), + ((sha("foo").digest(), sha("foo").digest()), 0), + ((sha("bar").digest(), sha("bar").digest()), 0) + ] def testKnown(self): - for pair, dist in self.known: - self.assertEqual(distance(pair[0], pair[1]), dist) + for pair, dist in self.known: + self.assertEqual(distance(pair[0], pair[1]), dist) def testCommutitive(self): - for i in xrange(100): - x, y, z = newID(), newID(), newID() - self.assertEqual(distance(x,y) ^ distance(y, z), distance(x, z)) - + for i in xrange(100): + x, y, z = newID(), newID(), newID() + self.assertEqual(distance(x,y) ^ distance(y, z), distance(x, z)) + class RandRange(unittest.TestCase): def testOnce(self): - a = intify(newID()) - b = intify(newID()) - if a < b: - c = randRange(a, b) - self.assertEqual(a <= c < b, 1, "output out of range %d %d %d" % (b, c, a)) - else: - c = randRange(b, a) - assert b <= c < a, "output out of range %d %d %d" % (b, c, a) + a = intify(newID()) + b = intify(newID()) + if a < b: + c = randRange(a, b) + self.assertEqual(a <= c < b, 1, "output out of range %d %d %d" % (b, c, a)) + else: + c = randRange(b, a) + assert b <= c < a, "output out of range %d %d %d" % (b, c, a) def testOneHundredTimes(self): - for i in xrange(100): - self.testOnce() + for i in xrange(100): + self.testOnce() if __name__ == '__main__': unittest.main() - \ No newline at end of file + -- 2.39.5