summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSybren A. St?vel <sybren@stuvel.eu>2009-01-22 15:56:06 +0100
committerSybren A. St?vel <sybren@stuvel.eu>2009-01-22 15:56:06 +0100
commita9c5f3f1a88de1c5ecd08b18958c4b064aed2506 (patch)
treeb2c13279beb4390a577caf075cb18f8a1ffde4bf
parent82f86714fecfe6a93695b679780c102c6f4109af (diff)
downloadrsa-a9c5f3f1a88de1c5ecd08b18958c4b064aed2506.tar.gz
Use os.urandom instead of reading from /dev/urandom, bumped version to 1.3-beta0
-rw-r--r--rsa/__init__.py41
-rwxr-xr-xsetup.py2
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:
diff --git a/setup.py b/setup.py
index fa73a4b..2de7271 100755
--- a/setup.py
+++ b/setup.py
@@ -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',