Skip to content

Commit dd56e07

Browse files
committed
Fixed stream bug (broke due to optional padding option). See issue #8.
1 parent 5518ae8 commit dd56e07

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

pyaes/blockfeeder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ def _feed_stream(feeder, in_stream, out_stream, block_size = BLOCK_SIZE):
216216
def encrypt_stream(mode, in_stream, out_stream, block_size = BLOCK_SIZE, padding = PADDING_DEFAULT):
217217
'Encrypts a stream of bytes from in_stream to out_stream using mode.'
218218

219-
encrypter = Encrypter(mode)
220-
_feed_stream(encrypter, in_stream, out_stream, block_size, padding)
219+
encrypter = Encrypter(mode, padding = padding)
220+
_feed_stream(encrypter, in_stream, out_stream, block_size)
221221

222222

223223
def decrypt_stream(mode, in_stream, out_stream, block_size = BLOCK_SIZE, padding = PADDING_DEFAULT):
224224
'Decrypts a stream of bytes from in_stream to out_stream using mode.'
225225

226-
decrypter = Decrypter(mode)
227-
_feed_stream(decrypter, in_stream, out_stream, block_size, padding)
226+
decrypter = Decrypter(mode, padding = padding)
227+
_feed_stream(decrypter, in_stream, out_stream, block_size)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
for API reference and details.'''
99

1010
setup(name = 'pyaes',
11-
version = '1.5.0',
11+
version = '1.6.0',
1212
description = 'Pure-Python Implementation of the AES block-cipher and common modes of operation',
1313
long_description = LONG_DESCRIPTION,
1414
author = 'Richard Moore',

tests/test-blockfeeder.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@
2727
import os
2828
import random
2929

30+
try:
31+
from StringIO import StringIO
32+
except:
33+
import io
34+
StringIO = io.BytesIO
35+
3036
import pyaes
3137
from pyaes.blockfeeder import Decrypter, Encrypter
32-
38+
from pyaes import decrypt_stream, encrypt_stream
3339
from pyaes.util import to_bufferable
3440

3541

@@ -114,3 +120,29 @@
114120
passed = decrypted == plaintext
115121
cipher_length = len(ciphertext)
116122
print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals())
123+
124+
plaintext = os.urandom(1000)
125+
126+
for mode_name in pyaes.AESModesOfOperation:
127+
mode = pyaes.AESModesOfOperation[mode_name]
128+
print(mode.name + ' (stream operations)')
129+
130+
kw = dict(key = key)
131+
if mode_name in ('cbc', 'cfb', 'ofb'):
132+
kw['iv'] = os.urandom(16)
133+
134+
moo = mode(**kw)
135+
output = StringIO()
136+
pyaes.encrypt_stream(moo, StringIO(plaintext), output)
137+
output.seek(0)
138+
ciphertext = output.read()
139+
140+
moo = mode(**kw)
141+
output = StringIO()
142+
pyaes.decrypt_stream(moo, StringIO(ciphertext), output)
143+
output.seek(0)
144+
decrypted = output.read()
145+
146+
passed = decrypted == plaintext
147+
cipher_length = len(ciphertext)
148+
print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals())

0 commit comments

Comments
 (0)