Skip to content

Commit 03c51e7

Browse files
committed
Parellelized testing. Caught a lot of bugs.
1 parent f9981d2 commit 03c51e7

23 files changed

Lines changed: 235 additions & 208 deletions

rsa/_compat.py

Lines changed: 1 addition & 6 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-
'''Python compatibility wrappers.'''
17+
"""Python compatibility wrappers."""
1818

1919

2020
from __future__ import absolute_import
@@ -108,11 +108,6 @@ def byte(num):
108108
Use it as a replacement for ``chr`` where you are expecting a byte
109109
because this will work on all current versions of Python::
110110
111-
>>> byte(0)
112-
'\x00'
113-
>>> byte(255)
114-
'\xff'
115-
116111
:param num:
117112
An unsigned integer between 0 and 255 (both inclusive).
118113
:returns:

rsa/bigfile.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
'''Large file support
16+
"""Large file support
1717
1818
- break a file into smaller blocks, and encrypt them, and store the
1919
encrypted blocks in another file.
@@ -36,19 +36,19 @@
3636
This file format is called the VARBLOCK format, in line with the varint format
3737
used to denote the block sizes.
3838
39-
'''
39+
"""
4040

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

4444
def encrypt_bigfile(infile, outfile, pub_key):
45-
'''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
45+
"""Encrypts a file, writing it to 'outfile' in VARBLOCK format.
4646
4747
:param infile: file-like object to read the cleartext from
4848
:param outfile: file-like object to write the crypto in VARBLOCK format to
4949
:param pub_key: :py:class:`rsa.PublicKey` to encrypt with
5050
51-
'''
51+
"""
5252

5353
if not isinstance(pub_key, key.PublicKey):
5454
raise TypeError('Public key required, but got %r' % pub_key)
@@ -67,13 +67,13 @@ def encrypt_bigfile(infile, outfile, pub_key):
6767
outfile.write(crypto)
6868

6969
def decrypt_bigfile(infile, outfile, priv_key):
70-
'''Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
70+
"""Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
7171
7272
:param infile: file-like object to read the crypto in VARBLOCK format from
7373
:param outfile: file-like object to write the cleartext to
7474
:param priv_key: :py:class:`rsa.PrivateKey` to decrypt with
7575
76-
'''
76+
"""
7777

7878
if not isinstance(priv_key, key.PrivateKey):
7979
raise TypeError('Private key required, but got %r' % priv_key)

rsa/cli.py

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

17-
'''Commandline scripts.
17+
"""Commandline scripts.
1818
1919
These scripts are called by the executables defined in setup.py.
20-
'''
20+
"""
21+
22+
from __future__ import with_statement
2123

2224
import abc
2325
import sys
@@ -30,7 +32,7 @@
3032
HASH_METHODS = sorted(rsa.pkcs1.HASH_METHODS.keys())
3133

3234
def keygen():
33-
'''Key generator.'''
35+
"""Key generator."""
3436

3537
# Parse the CLI options
3638
parser = OptionParser(usage='usage: %prog [options] keysize',
@@ -86,7 +88,7 @@ def keygen():
8688

8789

8890
class CryptoOperation(object):
89-
'''CLI callable that operates with input, output, and a key.'''
91+
"""CLI callable that operates with input, output, and a key."""
9092

9193
__metaclass__ = abc.ABCMeta
9294

@@ -112,15 +114,15 @@ def __init__(self):
112114

113115
@abc.abstractmethod
114116
def perform_operation(self, indata, key, cli_args=None):
115-
'''Performs the program's operation.
117+
"""Performs the program's operation.
116118
117119
Implement in a subclass.
118120
119121
:returns: the data to write to the output.
120-
'''
122+
"""
121123

122124
def __call__(self):
123-
'''Runs the program.'''
125+
"""Runs the program."""
124126

125127
(cli, cli_args) = self.parse_cli()
126128

@@ -135,10 +137,10 @@ def __call__(self):
135137
self.write_outfile(outdata, cli.output)
136138

137139
def parse_cli(self):
138-
'''Parse the CLI options
140+
"""Parse the CLI options
139141
140142
:returns: (cli_opts, cli_args)
141-
'''
143+
"""
142144

143145
parser = OptionParser(usage=self.usage, description=self.description)
144146

@@ -160,7 +162,7 @@ def parse_cli(self):
160162
return (cli, cli_args)
161163

162164
def read_key(self, filename, keyform):
163-
'''Reads a public or private key.'''
165+
"""Reads a public or private key."""
164166

165167
print >>sys.stderr, 'Reading %s key from %s' % (self.keyname, filename)
166168
with open(filename) as keyfile:
@@ -169,7 +171,7 @@ def read_key(self, filename, keyform):
169171
return self.key_class.load_pkcs1(keydata, keyform)
170172

171173
def read_infile(self, inname):
172-
'''Read the input file'''
174+
"""Read the input file"""
173175

174176
if inname:
175177
print >>sys.stderr, 'Reading input from %s' % inname
@@ -180,7 +182,7 @@ def read_infile(self, inname):
180182
return sys.stdin.read()
181183

182184
def write_outfile(self, outdata, outname):
183-
'''Write the output file'''
185+
"""Write the output file"""
184186

185187
if outname:
186188
print >>sys.stderr, 'Writing output to %s' % outname
@@ -191,7 +193,7 @@ def write_outfile(self, outdata, outname):
191193
sys.stdout.write(outdata)
192194

193195
class EncryptOperation(CryptoOperation):
194-
'''Encrypts a file.'''
196+
"""Encrypts a file."""
195197

196198
keyname = 'public'
197199
description = ('Encrypts a file. The file must be shorter than the key '
@@ -203,12 +205,12 @@ class EncryptOperation(CryptoOperation):
203205

204206

205207
def perform_operation(self, indata, pub_key, cli_args=None):
206-
'''Encrypts files.'''
208+
"""Encrypts files."""
207209

208210
return rsa.encrypt(indata, pub_key)
209211

210212
class DecryptOperation(CryptoOperation):
211-
'''Decrypts a file.'''
213+
"""Decrypts a file."""
212214

213215
keyname = 'private'
214216
description = ('Decrypts a file. The original file must be shorter than '
@@ -220,12 +222,12 @@ class DecryptOperation(CryptoOperation):
220222
key_class = rsa.PrivateKey
221223

222224
def perform_operation(self, indata, priv_key, cli_args=None):
223-
'''Decrypts files.'''
225+
"""Decrypts files."""
224226

225227
return rsa.decrypt(indata, priv_key)
226228

227229
class SignOperation(CryptoOperation):
228-
'''Signs a file.'''
230+
"""Signs a file."""
229231

230232
keyname = 'private'
231233
usage = 'usage: %%prog [options] private_key hash_method'
@@ -241,7 +243,7 @@ class SignOperation(CryptoOperation):
241243
'to stdout if this option is not present.')
242244

243245
def perform_operation(self, indata, priv_key, cli_args):
244-
'''Decrypts files.'''
246+
"""Decrypts files."""
245247

246248
hash_method = cli_args[1]
247249
if hash_method not in HASH_METHODS:
@@ -251,7 +253,7 @@ def perform_operation(self, indata, priv_key, cli_args):
251253
return rsa.sign(indata, priv_key, hash_method)
252254

253255
class VerifyOperation(CryptoOperation):
254-
'''Verify a signature.'''
256+
"""Verify a signature."""
255257

256258
keyname = 'public'
257259
usage = 'usage: %%prog [options] private_key signature_file'
@@ -265,7 +267,7 @@ class VerifyOperation(CryptoOperation):
265267
has_output = False
266268

267269
def perform_operation(self, indata, pub_key, cli_args):
268-
'''Decrypts files.'''
270+
"""Decrypts files."""
269271

270272
signature_file = cli_args[1]
271273

@@ -281,21 +283,21 @@ def perform_operation(self, indata, pub_key, cli_args):
281283

282284

283285
class BigfileOperation(CryptoOperation):
284-
'''CryptoOperation that doesn't read the entire file into memory.'''
286+
"""CryptoOperation that doesn't read the entire file into memory."""
285287

286288
def __init__(self):
287289
CryptoOperation.__init__(self)
288290

289291
self.file_objects = []
290292

291293
def __del__(self):
292-
'''Closes any open file handles.'''
294+
"""Closes any open file handles."""
293295

294296
for fobj in self.file_objects:
295297
fobj.close()
296298

297299
def __call__(self):
298-
'''Runs the program.'''
300+
"""Runs the program."""
299301

300302
(cli, cli_args) = self.parse_cli()
301303

@@ -310,7 +312,7 @@ def __call__(self):
310312
self.perform_operation(infile, outfile, key, cli_args)
311313

312314
def get_infile(self, inname):
313-
'''Returns the input file object'''
315+
"""Returns the input file object"""
314316

315317
if inname:
316318
print >>sys.stderr, 'Reading input from %s' % inname
@@ -323,7 +325,7 @@ def get_infile(self, inname):
323325
return fobj
324326

325327
def get_outfile(self, outname):
326-
'''Returns the output file object'''
328+
"""Returns the output file object"""
327329

328330
if outname:
329331
print >>sys.stderr, 'Will write output to %s' % outname
@@ -336,7 +338,7 @@ def get_outfile(self, outname):
336338
return fobj
337339

338340
class EncryptBigfileOperation(BigfileOperation):
339-
'''Encrypts a file to VARBLOCK format.'''
341+
"""Encrypts a file to VARBLOCK format."""
340342

341343
keyname = 'public'
342344
description = ('Encrypts a file to an encrypted VARBLOCK file. The file '
@@ -347,12 +349,12 @@ class EncryptBigfileOperation(BigfileOperation):
347349
operation_progressive = 'encrypting'
348350

349351
def perform_operation(self, infile, outfile, pub_key, cli_args=None):
350-
'''Encrypts files to VARBLOCK.'''
352+
"""Encrypts files to VARBLOCK."""
351353

352354
return rsa.bigfile.encrypt_bigfile(infile, outfile, pub_key)
353355

354356
class DecryptBigfileOperation(BigfileOperation):
355-
'''Decrypts a file in VARBLOCK format.'''
357+
"""Decrypts a file in VARBLOCK format."""
356358

357359
keyname = 'private'
358360
description = ('Decrypts an encrypted VARBLOCK file that was encrypted '
@@ -363,7 +365,7 @@ class DecryptBigfileOperation(BigfileOperation):
363365
key_class = rsa.PrivateKey
364366

365367
def perform_operation(self, infile, outfile, priv_key, cli_args=None):
366-
'''Decrypts a VARBLOCK file.'''
368+
"""Decrypts a VARBLOCK file."""
367369

368370
return rsa.bigfile.decrypt_bigfile(infile, outfile, priv_key)
369371

rsa/common.py

Lines changed: 11 additions & 32 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-
'''Common functionality shared by several modules.'''
17+
"""Common functionality shared by several modules."""
1818

1919

2020
def bit_size(num):
@@ -61,29 +61,14 @@ def bit_size(num):
6161

6262

6363
def _bit_size(number):
64-
'''Returns the number of bits required to hold a specific long number.
65-
66-
>>> bit_size(1023)
67-
10
68-
>>> bit_size(1024)
69-
11
70-
>>> bit_size(1025)
71-
11
72-
73-
>>> bit_size(1 << 1024)
74-
1025
75-
>>> bit_size((1 << 1024) + 1)
76-
1025
77-
>>> bit_size((1 << 1024) - 1)
78-
1024
79-
80-
'''
81-
64+
"""
65+
Returns the number of bits required to hold a specific long number.
66+
"""
8267
if number < 0:
8368
raise ValueError('Only nonnegative numbers possible: %s' % number)
8469

8570
if number == 0:
86-
return 1
71+
return 0
8772

8873
# This works, even with very large numbers. When using math.log(number, 2),
8974
# you'll get rounding errors and it'll fail.
@@ -99,15 +84,9 @@ def byte_size(number):
9984
"""Returns the number of bytes required to hold a specific long number.
10085
10186
The number of bytes is rounded up.
102-
103-
>>> byte_size(1 << 1023)
104-
128
105-
>>> byte_size((1 << 1024) - 1)
106-
128
107-
>>> byte_size(1 << 1024)
108-
129
10987
"""
110-
88+
if number == 0:
89+
return 0
11190
# Does not perform floating-point division and uses built-in divmod
11291
# operator.
11392
quanta, mod = divmod(bit_size(number), 8)
@@ -140,13 +119,13 @@ def extended_gcd(a, b):
140119
return (a, lx, ly) #Return only positive values
141120

142121
def inverse(x, n):
143-
'''Returns x^-1 (mod n)
122+
"""Returns x^-1 (mod n)
144123
145124
>>> inverse(7, 4)
146125
3
147126
>>> (inverse(143, 4) * 143) % 4
148127
1
149-
'''
128+
"""
150129

151130
(divider, inv, _) = extended_gcd(x, n)
152131

@@ -157,7 +136,7 @@ def inverse(x, n):
157136

158137

159138
def crt(a_values, modulo_values):
160-
'''Chinese Remainder Theorem.
139+
"""Chinese Remainder Theorem.
161140
162141
Calculates x such that x = a[i] (mod m[i]) for each i.
163142
@@ -174,7 +153,7 @@ def crt(a_values, modulo_values):
174153
175154
>>> crt([2, 3, 0], [7, 11, 15])
176155
135
177-
'''
156+
"""
178157

179158
m = 1
180159
x = 0

0 commit comments

Comments
 (0)