diff options
author | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-13 06:00:31 +0530 |
---|---|---|
committer | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-13 06:00:31 +0530 |
commit | 60f064b0efa7fa94cfc663755aa0cf091ed8ba02 (patch) | |
tree | b9be62d96d084b8dd6fd9b09977bcc3c02537b03 | |
parent | e4499dbd73502f251e7b29d551bae7b333455114 (diff) | |
download | rsa-60f064b0efa7fa94cfc663755aa0cf091ed8ba02.tar.gz |
Measure twice, cut once.
-rw-r--r-- | rsa/transform.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/rsa/transform.py b/rsa/transform.py index 396493e..fe4def8 100644 --- a/rsa/transform.py +++ b/rsa/transform.py @@ -174,13 +174,12 @@ def int2bytes(number, chunk_size=0, for zero_leading, x in enumerate(raw_bytes): if x != _zero_byte[0]: break - raw_bytes = raw_bytes[zero_leading:] if chunk_size > 0: # Bounds checking. We're not doing this up-front because the # most common use case is not specifying a chunk size. In the worst # case, the number will already have been converted to bytes above. - length = len(raw_bytes) + length = len(raw_bytes) - zero_leading if length > chunk_size: raise OverflowError( "Need %d bytes for number, but chunk size is %d" % @@ -188,7 +187,13 @@ def int2bytes(number, chunk_size=0, ) remainder = length % chunk_size if remainder: - raw_bytes = (chunk_size - remainder) * _zero_byte + raw_bytes + padding_size = (chunk_size - remainder) + if zero_leading > 0: + raw_bytes = raw_bytes[zero_leading-padding_size:] + else: + raw_bytes = (padding_size * _zero_byte) + raw_bytes + else: + raw_bytes = raw_bytes[zero_leading:] return raw_bytes |