Skip to content

Commit 8d6bdf4

Browse files
committed
Finally tracked down the bug to incorrect padding. Now all tests should pass
for the new int2bytes.
1 parent ce4ede7 commit 8d6bdf4

1 file changed

Lines changed: 3 additions & 14 deletions

File tree

rsa/transform.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,6 @@ def int2bytes(number, chunk_size=0,
153153
``OverflowError`` when block_size is given and the number takes up more
154154
bytes than fit into the block.
155155
"""
156-
# Machine word-aligned implementation.
157-
# ~19x faster than naive implementation on 32-bit processors.
158-
# ~33x faster than naive implementation on 64-bit processors.
159-
# ~50x faster on 64-bit pypy 1.5
160-
161-
# Don't need to raise TypeError ourselves. The code does that already
162-
# if a bad type is passed in as argument.
163-
164156
if number < 0:
165157
raise ValueError('Number must be unsigned integer: %d' % number)
166158

@@ -182,24 +174,21 @@ def int2bytes(number, chunk_size=0,
182174
for zero_leading, x in enumerate(raw_bytes):
183175
if x != _zero_byte[0]:
184176
break
177+
raw_bytes = raw_bytes[zero_leading:]
185178

186179
if chunk_size > 0:
187180
# Bounds checking. We're not doing this up-front because the
188181
# most common use case is not specifying a chunk size. In the worst
189182
# case, the number will already have been converted to bytes above.
190-
#length = count * word_bytes
191183
length = len(raw_bytes)
192-
bytes_needed = length - zero_leading
193-
if bytes_needed > chunk_size:
184+
if length > chunk_size:
194185
raise OverflowError(
195186
"Need %d bytes for number, but chunk size is %d" %
196-
(bytes_needed, chunk_size)
187+
(length, chunk_size)
197188
)
198189
remainder = length % chunk_size
199190
if remainder:
200191
raw_bytes = (chunk_size - remainder) * _zero_byte + raw_bytes
201-
else:
202-
raw_bytes = raw_bytes[zero_leading:]
203192
return raw_bytes
204193

205194

0 commit comments

Comments
 (0)