Updated doctests to Python 3.5 and automatically running with Tox.
I've also removed doctests from the obsolete rsa/_versionXXX.py files,
as those files aren't even compatible with Python 3.x anyway.
diff --git a/create_timing_table.py b/create_timing_table.py
index 48cd1ff..1c31b98 100755
--- a/create_timing_table.py
+++ b/create_timing_table.py
@@ -39,5 +39,6 @@
(bitsize, dur_per_call, iterations, duration))
-for bitsize in (128, 256, 384, 512, 1024, 2048, 3072, 4096):
- run_speed_test(bitsize)
+if __name__ == '__main__':
+ for bitsize in (128, 256, 384, 512, 1024, 2048, 3072, 4096):
+ run_speed_test(bitsize)
diff --git a/rsa/_version133.py b/rsa/_version133.py
index f059a18..b30b695 100644
--- a/rsa/_version133.py
+++ b/rsa/_version133.py
@@ -67,12 +67,6 @@
def bytes2int(bytes):
"""Converts a list of bytes or a string to an integer
-
- >>> (128*256 + 64)*256 + + 15
- 8405007
- >>> l = [128, 64, 15]
- >>> bytes2int(l)
- 8405007
"""
if not (type(bytes) is types.ListType or type(bytes) is types.StringType):
@@ -89,9 +83,6 @@
def int2bytes(number):
"""Converts a number to a string of bytes
-
- >>> bytes2int(int2bytes(123456789))
- 123456789
"""
if not (type(number) is types.LongType or type(number) is types.IntType):
@@ -208,11 +199,6 @@
def is_prime(number):
"""Returns True if the number is prime, and False otherwise.
-
- >>> is_prime(42)
- 0
- >>> is_prime(41)
- 1
"""
"""
@@ -232,14 +218,6 @@
def getprime(nbits):
"""Returns a prime number of max. 'math.ceil(nbits/8)*8' bits. In
other words: nbits is rounded up to whole bytes.
-
- >>> p = getprime(8)
- >>> is_prime(p-1)
- 0
- >>> is_prime(p)
- 1
- >>> is_prime(p+1)
- 0
"""
nbytes = int(math.ceil(nbits/8.))
@@ -260,11 +238,6 @@
def are_relatively_prime(a, b):
"""Returns True if a and b are relatively prime, and False if they
are not.
-
- >>> are_relatively_prime(2, 3)
- 1
- >>> are_relatively_prime(2, 4)
- 0
"""
d = gcd(a, b)
diff --git a/rsa/_version200.py b/rsa/_version200.py
index eba0d27..717b6ac 100644
--- a/rsa/_version200.py
+++ b/rsa/_version200.py
@@ -60,12 +60,6 @@
def bytes2int(bytes):
r"""Converts a list of bytes or a string to an integer
-
- >>> (((128 * 256) + 64) * 256) + 15
- 8405007
- >>> l = [128, 64, 15]
- >>> bytes2int(l) #same as bytes2int('\x80@\x0f')
- 8405007
"""
if not (type(bytes) is types.ListType or type(bytes) is types.StringType):
@@ -99,9 +93,6 @@
def to64(number):
"""Converts a number in the range of 0 to 63 into base 64 digit
character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'.
-
- >>> to64(10)
- 'A'
"""
if not (type(number) is types.LongType or type(number) is types.IntType):
@@ -128,9 +119,6 @@
def from64(number):
"""Converts an ordinal character value in the range of
0-9,A-Z,a-z,-,_ to a number in the range of 0-63.
-
- >>> from64(49)
- 1
"""
if not (type(number) is types.LongType or type(number) is types.IntType):
@@ -157,9 +145,6 @@
def int2str64(number):
"""Converts a number to a string of base64 encoded characters in
the range of '0'-'9','A'-'Z,'a'-'z','-','_'.
-
- >>> int2str64(123456789)
- '7MyqL'
"""
if not (type(number) is types.LongType or type(number) is types.IntType):
@@ -177,9 +162,6 @@
def str642int(string):
"""Converts a base64 encoded string into an integer.
The chars of this string in in the range '0'-'9','A'-'Z','a'-'z','-','_'
-
- >>> str642int('7MyqL')
- 123456789
"""
if not (type(string) is types.ListType or type(string) is types.StringType):
@@ -270,11 +252,6 @@
def is_prime(number):
"""Returns True if the number is prime, and False otherwise.
-
- >>> is_prime(42)
- 0
- >>> is_prime(41)
- 1
"""
if randomized_primality_testing(number, 6):
@@ -288,14 +265,6 @@
def getprime(nbits):
"""Returns a prime number of max. 'math.ceil(nbits/8)*8' bits. In
other words: nbits is rounded up to whole bytes.
-
- >>> p = getprime(8)
- >>> is_prime(p-1)
- 0
- >>> is_prime(p)
- 1
- >>> is_prime(p+1)
- 0
"""
while True:
diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py
index fdbbb8b..7d6814c 100644
--- a/rsa/pkcs1.py
+++ b/rsa/pkcs1.py
@@ -69,13 +69,13 @@
:return: 00 02 RANDOM_DATA 00 MESSAGE
- >>> block = _pad_for_encryption('hello', 16)
+ >>> block = _pad_for_encryption(b'hello', 16)
>>> len(block)
16
>>> block[0:2]
- '\x00\x02'
+ b'\x00\x02'
>>> block[-6:]
- '\x00hello'
+ b'\x00hello'
"""
@@ -117,15 +117,15 @@
:return: 00 01 PADDING 00 MESSAGE
- >>> block = _pad_for_signing('hello', 16)
+ >>> block = _pad_for_signing(b'hello', 16)
>>> len(block)
16
>>> block[0:2]
- '\x00\x01'
+ b'\x00\x01'
>>> block[-6:]
- '\x00hello'
+ b'\x00hello'
>>> block[2:-6]
- '\xff\xff\xff\xff\xff\xff\xff\xff'
+ b'\xff\xff\xff\xff\xff\xff\xff\xff'
"""
@@ -156,7 +156,7 @@
>>> from rsa import key, common
>>> (pub_key, priv_key) = key.newkeys(256)
- >>> message = 'hello'
+ >>> message = b'hello'
>>> crypto = encrypt(message, pub_key)
The crypto text should be just as long as the public key 'n' component:
@@ -195,15 +195,15 @@
It works with strings:
- >>> crypto = encrypt('hello', pub_key)
+ >>> crypto = encrypt(b'hello', pub_key)
>>> decrypt(crypto, priv_key)
- 'hello'
+ b'hello'
And with binary data:
- >>> crypto = encrypt('\x00\x00\x00\x00\x01', pub_key)
+ >>> crypto = encrypt(b'\x00\x00\x00\x00\x01', pub_key)
>>> decrypt(crypto, priv_key)
- '\x00\x00\x00\x00\x01'
+ b'\x00\x00\x00\x00\x01'
Altering the encrypted information will *likely* cause a
:py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use
@@ -218,12 +218,12 @@
It's only a tiny bit of information, but every bit makes cracking the
keys easier.
- >>> crypto = encrypt('hello', pub_key)
- >>> crypto = crypto[0:5] + 'X' + crypto[6:] # change a byte
+ >>> crypto = encrypt(b'hello', pub_key)
+ >>> crypto = crypto[0:5] + b'X' + crypto[6:] # change a byte
>>> decrypt(crypto, priv_key)
Traceback (most recent call last):
...
- DecryptionError: Decryption failed
+ rsa.pkcs1.DecryptionError: Decryption failed
"""
diff --git a/rsa/transform.py b/rsa/transform.py
index 889b3f2..758dea4 100644
--- a/rsa/transform.py
+++ b/rsa/transform.py
@@ -44,7 +44,7 @@
>>> (((128 * 256) + 64) * 256) + 15
8405007
- >>> bytes2int('\x80@\x0f')
+ >>> bytes2int(b'\x80@\x0f')
8405007
"""
@@ -58,12 +58,12 @@
Usage::
>>> _int2bytes(123456789)
- '\x07[\xcd\x15'
+ b'\x07[\xcd\x15'
>>> bytes2int(_int2bytes(123456789))
123456789
>>> _int2bytes(123456789, 6)
- '\x00\x00\x07[\xcd\x15'
+ b'\x00\x00\x07[\xcd\x15'
>>> bytes2int(_int2bytes(123456789, 128))
123456789
diff --git a/setup.py b/setup.py
index d46945b..428b14c 100755
--- a/setup.py
+++ b/setup.py
@@ -16,39 +16,40 @@
from setuptools import setup
-setup(name='rsa',
- version='3.3',
- description='Pure-Python RSA implementation',
- author='Sybren A. Stuvel',
- author_email='sybren@stuvel.eu',
- maintainer='Sybren A. Stuvel',
- maintainer_email='sybren@stuvel.eu',
- url='https://stuvel.eu/rsa',
- packages=['rsa'],
- license='ASL 2',
- classifiers=[
- 'Development Status :: 5 - Production/Stable',
- 'Intended Audience :: Developers',
- 'Intended Audience :: Education',
- 'Intended Audience :: Information Technology',
- 'License :: OSI Approved :: Apache Software License',
- 'Operating System :: OS Independent',
- 'Programming Language :: Python',
- 'Programming Language :: Python :: 3',
- 'Topic :: Security :: Cryptography',
- ],
- install_requires=[
- 'pyasn1 >= 0.1.3',
- ],
- entry_points={'console_scripts': [
- 'pyrsa-priv2pub = rsa.util:private_to_public',
- 'pyrsa-keygen = rsa.cli:keygen',
- 'pyrsa-encrypt = rsa.cli:encrypt',
- 'pyrsa-decrypt = rsa.cli:decrypt',
- 'pyrsa-sign = rsa.cli:sign',
- 'pyrsa-verify = rsa.cli:verify',
- 'pyrsa-encrypt-bigfile = rsa.cli:encrypt_bigfile',
- 'pyrsa-decrypt-bigfile = rsa.cli:decrypt_bigfile',
- ]},
+if __name__ == '__main__':
+ setup(name='rsa',
+ version='3.3',
+ description='Pure-Python RSA implementation',
+ author='Sybren A. Stuvel',
+ author_email='sybren@stuvel.eu',
+ maintainer='Sybren A. Stuvel',
+ maintainer_email='sybren@stuvel.eu',
+ url='https://stuvel.eu/rsa',
+ packages=['rsa'],
+ license='ASL 2',
+ classifiers=[
+ 'Development Status :: 5 - Production/Stable',
+ 'Intended Audience :: Developers',
+ 'Intended Audience :: Education',
+ 'Intended Audience :: Information Technology',
+ 'License :: OSI Approved :: Apache Software License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 3',
+ 'Topic :: Security :: Cryptography',
+ ],
+ install_requires=[
+ 'pyasn1 >= 0.1.3',
+ ],
+ entry_points={'console_scripts': [
+ 'pyrsa-priv2pub = rsa.util:private_to_public',
+ 'pyrsa-keygen = rsa.cli:keygen',
+ 'pyrsa-encrypt = rsa.cli:encrypt',
+ 'pyrsa-decrypt = rsa.cli:decrypt',
+ 'pyrsa-sign = rsa.cli:sign',
+ 'pyrsa-verify = rsa.cli:verify',
+ 'pyrsa-encrypt-bigfile = rsa.cli:encrypt_bigfile',
+ 'pyrsa-decrypt-bigfile = rsa.cli:decrypt_bigfile',
+ ]},
- )
+ )
diff --git a/tox.ini b/tox.ini
index 5932192..f59d647 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,7 +3,6 @@
[pytest]
addopts = -v --cov rsa --cov-report term-missing
-# --doctest-modules
[testenv]
setenv =
@@ -14,3 +13,6 @@
PyTest
pytest-xdist
pytest-cov
+
+[testenv:py35]
+commands=py.test --doctest-modules []