From dead3eff408396611d9f0dd7f8a3fc09cc097ae8 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Sat, 14 Jun 2008 16:24:21 -0700 Subject: [PATCH] Allow the setting of khashmir ID's suffix. --- apt_p2p_Khashmir/khash.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/apt_p2p_Khashmir/khash.py b/apt_p2p_Khashmir/khash.py index 3a763d1..65670af 100644 --- a/apt_p2p_Khashmir/khash.py +++ b/apt_p2p_Khashmir/khash.py @@ -30,11 +30,18 @@ def distance(a, b): """Calculate the distance between two hashes expressed as strings.""" return intify(a) ^ intify(b) -def newID(): - """Get a new pseudorandom globally unique hash string.""" +def newID(suffix = ''): + """Get a new pseudorandom globally unique hash string. + + @param suffix: an optional string to end the ID with + """ + assert len(suffix) < 20, 'The suffix must be shorter than the SHA1 hash' h = sha() h.update(urandom(HASH_LENGTH)) - return h.digest() + if suffix: + return h.digest()[:-len(suffix)] + suffix + else: + return h.digest() def newIDInRange(min, max): """Get a new pseudorandom globally unique hash string in the range.""" @@ -54,7 +61,11 @@ class TestNewID(unittest.TestCase): self.failUnlessEqual(len(newID()), HASH_LENGTH) def testHundreds(self): for x in xrange(100): - self.testLength + self.testLength() + def testSuffix(self): + id = newID('T012') + self.failUnlessEqual(len(id), HASH_LENGTH) + self.failUnlessEqual(id[-4:], 'T012') class TestIntify(unittest.TestCase): """Test the intify function.""" -- 2.39.5