Skip to content

Commit d3d1034

Browse files
committed
Big refactor to become more PEP8 compliant.
Mostly focused on docstrings (''' → """), indentation, empty lines, and superfluous parenthesis.
1 parent 541ee46 commit d3d1034

27 files changed

Lines changed: 486 additions & 490 deletions

create_timing_table.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
poolsize = 8
2222
accurate = True
2323

24-
def run_speed_test(bitsize):
2524

25+
def run_speed_test(bitsize):
2626
iterations = 0
2727
start = end = time.time()
2828

@@ -35,10 +35,9 @@ def run_speed_test(bitsize):
3535
duration = end - start
3636
dur_per_call = duration / iterations
3737

38-
print '%5i bit: %9.3f sec. (%i iterations over %.1f seconds)' % (bitsize,
39-
dur_per_call, iterations, duration)
38+
print('%5i bit: %9.3f sec. (%i iterations over %.1f seconds)' %
39+
(bitsize, dur_per_call, iterations, duration))
40+
4041

4142
for bitsize in (128, 256, 384, 512, 1024, 2048, 3072, 4096):
4243
run_speed_test(bitsize)
43-
44-

rsa/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727
2828
"""
2929

30-
__author__ = "Sybren Stuvel, Barry Mead and Yesudeep Mangalapilly"
31-
__date__ = "2016-01-13"
32-
__version__ = '3.3'
33-
3430
from rsa.key import newkeys, PrivateKey, PublicKey
3531
from rsa.pkcs1 import encrypt, decrypt, sign, verify, DecryptionError, \
3632
VerificationError
3733

34+
__author__ = "Sybren Stuvel, Barry Mead and Yesudeep Mangalapilly"
35+
__date__ = "2016-01-13"
36+
__version__ = '3.3'
37+
3838
# Do doctest if we're run directly
3939
if __name__ == "__main__":
4040
import doctest
41+
4142
doctest.testmod()
4243

4344
__all__ = ["newkeys", "encrypt", "decrypt", "sign", "verify", 'PublicKey',
44-
'PrivateKey', 'DecryptionError', 'VerificationError']
45-
45+
'PrivateKey', 'DecryptionError', 'VerificationError']

rsa/_compat.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
"""Python compatibility wrappers."""
1818

19-
2019
from __future__ import absolute_import
2120

2221
import sys
@@ -42,7 +41,6 @@
4241
# Else we just assume 64-bit processor keeping up with modern times.
4342
MACHINE_WORD_SIZE = 64
4443

45-
4644
try:
4745
# < Python3
4846
unicode_type = unicode
@@ -75,7 +73,6 @@ def byte_literal(s):
7573
# Python 2.5
7674
bytes_type = str
7775

78-
7976
# To avoid calling b() multiple times in tight loops.
8077
ZERO_BYTE = b('\x00')
8178
EMPTY_BYTE = b('')

rsa/asn1.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,40 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
'''ASN.1 definitions.
17+
"""ASN.1 definitions.
1818
1919
Not all ASN.1-handling code use these definitions, but when it does, they should be here.
20-
'''
20+
"""
2121

2222
from pyasn1.type import univ, namedtype, tag
2323

24+
2425
class PubKeyHeader(univ.Sequence):
2526
componentType = namedtype.NamedTypes(
26-
namedtype.NamedType('oid', univ.ObjectIdentifier()),
27-
namedtype.NamedType('parameters', univ.Null()),
27+
namedtype.NamedType('oid', univ.ObjectIdentifier()),
28+
namedtype.NamedType('parameters', univ.Null()),
2829
)
2930

31+
3032
class OpenSSLPubKey(univ.Sequence):
3133
componentType = namedtype.NamedTypes(
32-
namedtype.NamedType('header', PubKeyHeader()),
33-
34-
# This little hack (the implicit tag) allows us to get a Bit String as Octet String
35-
namedtype.NamedType('key', univ.OctetString().subtype(
36-
implicitTag=tag.Tag(tagClass=0, tagFormat=0, tagId=3))),
34+
namedtype.NamedType('header', PubKeyHeader()),
35+
36+
# This little hack (the implicit tag) allows us to get a Bit String as Octet String
37+
namedtype.NamedType('key', univ.OctetString().subtype(
38+
implicitTag=tag.Tag(tagClass=0, tagFormat=0, tagId=3))),
3739
)
3840

3941

4042
class AsnPubKey(univ.Sequence):
41-
'''ASN.1 contents of DER encoded public key:
42-
43+
"""ASN.1 contents of DER encoded public key:
44+
4345
RSAPublicKey ::= SEQUENCE {
4446
modulus INTEGER, -- n
4547
publicExponent INTEGER, -- e
46-
'''
48+
"""
4749

4850
componentType = namedtype.NamedTypes(
49-
namedtype.NamedType('modulus', univ.Integer()),
50-
namedtype.NamedType('publicExponent', univ.Integer()),
51+
namedtype.NamedType('modulus', univ.Integer()),
52+
namedtype.NamedType('publicExponent', univ.Integer()),
5153
)

rsa/bigfile.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
'''Large file support
17+
"""Large file support
1818
1919
- break a file into smaller blocks, and encrypt them, and store the
2020
encrypted blocks in another file.
@@ -37,25 +37,26 @@
3737
This file format is called the VARBLOCK format, in line with the varint format
3838
used to denote the block sizes.
3939
40-
'''
40+
"""
4141

4242
from rsa import key, common, pkcs1, varblock
4343
from rsa._compat import byte
4444

45+
4546
def encrypt_bigfile(infile, outfile, pub_key):
46-
'''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
47-
47+
"""Encrypts a file, writing it to 'outfile' in VARBLOCK format.
48+
4849
:param infile: file-like object to read the cleartext from
4950
:param outfile: file-like object to write the crypto in VARBLOCK format to
5051
:param pub_key: :py:class:`rsa.PublicKey` to encrypt with
5152
52-
'''
53+
"""
5354

5455
if not isinstance(pub_key, key.PublicKey):
5556
raise TypeError('Public key required, but got %r' % pub_key)
5657

5758
key_bytes = common.bit_size(pub_key.n) // 8
58-
blocksize = key_bytes - 11 # keep space for PKCS#1 padding
59+
blocksize = key_bytes - 11 # keep space for PKCS#1 padding
5960

6061
# Write the version number to the VARBLOCK file
6162
outfile.write(byte(varblock.VARBLOCK_VERSION))
@@ -67,21 +68,22 @@ def encrypt_bigfile(infile, outfile, pub_key):
6768
varblock.write_varint(outfile, len(crypto))
6869
outfile.write(crypto)
6970

71+
7072
def decrypt_bigfile(infile, outfile, priv_key):
71-
'''Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
72-
73+
"""Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
74+
7375
:param infile: file-like object to read the crypto in VARBLOCK format from
7476
:param outfile: file-like object to write the cleartext to
7577
:param priv_key: :py:class:`rsa.PrivateKey` to decrypt with
7678
77-
'''
79+
"""
7880

7981
if not isinstance(priv_key, key.PrivateKey):
8082
raise TypeError('Private key required, but got %r' % priv_key)
81-
83+
8284
for block in varblock.yield_varblocks(infile):
8385
cleartext = pkcs1.decrypt(block, priv_key)
8486
outfile.write(cleartext)
8587

86-
__all__ = ['encrypt_bigfile', 'decrypt_bigfile']
8788

89+
__all__ = ['encrypt_bigfile', 'decrypt_bigfile']

0 commit comments

Comments
 (0)