-## Copyright 2002 Andrew Loewenstern, All Rights Reserved
+## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
+# see LICENSE.txt for license information
from sha import sha
import whrandom
+
def intify(hstr):
"""20 bit hash, big-endian -> long python integer"""
assert len(hstr) == 20
return long(hstr.encode('hex'), 16)
-def stringify(int):
+def stringify(num):
"""long int -> 20-character string"""
- str = hex(int)[2:]
+ str = hex(num)[2:]
if str[-1] == 'L':
str = str[:-1]
if len(str) % 2 != 0:
"""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.randint(0,255)))
return h.digest()
def newIDInRange(min, max):
def randRange(min, max):
return min + intify(newID()) % (max - min)
-
+
### Test Cases ###
import unittest
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: