summaryrefslogtreecommitdiff
path: root/rsa/randnum.py
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2016-01-22 11:36:06 +0100
committerSybren A. Stüvel <sybren@stuvel.eu>2016-01-22 11:36:06 +0100
commitd3d10345b47c2b17922bb91059cfceea82f82338 (patch)
tree6a336d74ee41a4ba98b6b3d97f123cd0c5f4e9b7 /rsa/randnum.py
parent541ee468b6b33c7ae27818bbfea63df9622f9d8a (diff)
downloadrsa-git-d3d10345b47c2b17922bb91059cfceea82f82338.tar.gz
Big refactor to become more PEP8 compliant.
Mostly focused on docstrings (''' → """), indentation, empty lines, and superfluous parenthesis.
Diffstat (limited to 'rsa/randnum.py')
-rw-r--r--rsa/randnum.py20
1 files changed, 10 insertions, 10 deletions
diff --git a/rsa/randnum.py b/rsa/randnum.py
index 0e78274..2bb5806 100644
--- a/rsa/randnum.py
+++ b/rsa/randnum.py
@@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-'''Functions for generating random numbers.'''
+"""Functions for generating random numbers."""
# Source inspired by code by Yesudeep Mangalapilly <yesudeep@gmail.com>
@@ -23,12 +23,13 @@ import os
from rsa import common, transform
from rsa._compat import byte
+
def read_random_bits(nbits):
- '''Reads 'nbits' random bits.
+ """Reads 'nbits' random bits.
If nbits isn't a whole number of bytes, an extra byte will be appended with
only the lower bits set.
- '''
+ """
nbytes, rbits = divmod(nbits, 8)
@@ -45,8 +46,8 @@ def read_random_bits(nbits):
def read_random_int(nbits):
- '''Reads a random integer of approximately nbits bits.
- '''
+ """Reads a random integer of approximately nbits bits.
+ """
randomdata = read_random_bits(nbits)
value = transform.bytes2int(randomdata)
@@ -57,13 +58,14 @@ def read_random_int(nbits):
return value
+
def randint(maxvalue):
- '''Returns a random integer x with 1 <= x <= maxvalue
-
+ """Returns a random integer x with 1 <= x <= maxvalue
+
May take a very long time in specific situations. If maxvalue needs N bits
to store, the closer maxvalue is to (2 ** N) - 1, the faster this function
is.
- '''
+ """
bit_size = common.bit_size(maxvalue)
@@ -81,5 +83,3 @@ def randint(maxvalue):
tries += 1
return value
-
-