diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2009-01-22 15:56:06 +0100 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2009-01-22 15:56:06 +0100 |
commit | 721ab9951304bc809816e80160ebf11aa656139a (patch) | |
tree | b2c13279beb4390a577caf075cb18f8a1ffde4bf | |
parent | 93cb105992bc1febbb6f4dea8e968c151fb429e1 (diff) | |
download | rsa-git-721ab9951304bc809816e80160ebf11aa656139a.tar.gz |
Use os.urandom instead of reading from /dev/urandom, bumped version to 1.3-beta0
-rw-r--r-- | rsa/__init__.py | 41 | ||||
-rwxr-xr-x | setup.py | 2 |
2 files changed, 12 insertions, 31 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py index 996a7e2..24b4243 100644 --- a/rsa/__init__.py +++ b/rsa/__init__.py @@ -5,30 +5,20 @@ signing and verification. Includes generating public and private keys. """ __author__ = "Sybren Stuvel, Marloes de Boer and Ivo Tamboer" -__date__ = "2008-04-23" +__date__ = "2009-01-22" # NOTE: Python's modulo can return negative numbers. We compensate for # this behaviour using the abs() function +from cPickle import dumps, loads +import base64 import math +import os +import random import sys -import random # For picking semi-random numbers import types -from cPickle import dumps, loads -import base64 import zlib -RANDOM_DEV="/dev/urandom" -has_broken_randint = False - -try: - random.randint(1, 10000000000000000000000000L) -except: - has_broken_randint = True - print "This system's random.randint() can't handle large numbers" - print "Random integers will all be read from %s" % RANDOM_DEV - - def log(x, base = 10): return math.log(x) / math.log(base) @@ -97,32 +87,21 @@ def fast_exponentiation(a, p, n): return result def read_random_int(nbits): - """Reads a random integer from RANDOM_DEV of approximately nbits - bits rounded up to whole bytes""" + """Reads a random integer of approximately nbits bits rounded up + to whole bytes""" nbytes = ceil(nbits/8) - - # Read 'nbits' bits of random data - fd = open(RANDOM_DEV) - randomdata = fd.read(nbytes) - fd.close() - - if len(randomdata) != nbytes: - raise Exception("Unable to read enough random bytes") - + randomdata = os.urandom(nbytes) return bytes2int(randomdata) def ceil(x): - """Returns int(math.ceil(x))""" + """ceil(x) -> int(math.ceil(x))""" return int(math.ceil(x)) def randint(minvalue, maxvalue): """Returns a random integer x with minvalue <= x <= maxvalue""" - if not has_broken_randint: - return random.randint(minvalue, maxvalue) - # Safety - get a lot of random data even if the range is fairly # small min_nbits = 32 @@ -309,6 +288,8 @@ def calculate_keys(p, q, nbits): def gen_keys(nbits): """Generate RSA keys of nbits bits. Returns (p, q, e, d). + + Note: this can take a long time, depending on the key size. """ while True: @@ -3,7 +3,7 @@ from setuptools import setup setup(name='rsa', - version='1.2', + version='1.3-beta0', description='Pure-Python RSA implementation', author='Sybren Stuvel', author_email='sybren@stuvel.eu', |