ripped out xmlrpc, experimented with xmlrpc but with bencode, finally
[quix0rs-apt-p2p.git] / hash.py
diff --git a/hash.py b/hash.py
index bc6868668b00c48c27f4616179b703f2428ad4c0..2a312461155d21137407223553251f1b59e04162 100644 (file)
--- a/hash.py
+++ b/hash.py
@@ -3,14 +3,16 @@
 from sha import sha
 import whrandom
 
+random = open('/dev/urandom', 'r') # sucks for windoze
+
 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:
@@ -26,8 +28,7 @@ def distance(a, b):
 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(random.read(20))
     return h.digest()
 
 def newIDInRange(min, max):
@@ -35,7 +36,7 @@ def newIDInRange(min, max):
     
 def randRange(min, max):
     return min + intify(newID()) % (max - min)
-
+    
 
 ### Test Cases ###
 import unittest
@@ -49,7 +50,7 @@ class NewID(unittest.TestCase):
 
 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: