diff options
author | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-16 14:44:53 +0530 |
---|---|---|
committer | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-16 14:44:53 +0530 |
commit | 1caa13041d47213e8afba0ee8ccda67259203825 (patch) | |
tree | ad05d5e4d5915a7c7a3ca8bf4736c821aad8ae8e | |
parent | 03c51e75de8f9969f3fd5f3885a33ef04ce7348a (diff) | |
download | rsa-git-1caa13041d47213e8afba0ee8ccda67259203825.tar.gz |
Fixes bad zero-prefix padding bug in older _int2bytes to pass all tests.
-rw-r--r-- | rsa/transform.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/rsa/transform.py b/rsa/transform.py index 9ba2fe7..9abc6ae 100644 --- a/rsa/transform.py +++ b/rsa/transform.py @@ -33,10 +33,7 @@ except ImportError: import binascii from struct import pack from rsa import common -from rsa._compat import is_integer, b, byte, get_word_alignment - - -ZERO_BYTE = b('\x00') +from rsa._compat import is_integer, b, byte, get_word_alignment, ZERO_BYTE, EMPTY_BYTE def bytes2int(raw_bytes): @@ -74,14 +71,19 @@ def _int2bytes(number, block_size=0): raise ValueError('Negative numbers cannot be used: %i' % number) # Do some bounds checking - needed_bytes = common.byte_size(number) + if number == 0: + needed_bytes = 1 + raw_bytes = [ZERO_BYTE] + else: + needed_bytes = common.byte_size(number) + raw_bytes = [] + if block_size > 0: if needed_bytes > block_size: raise OverflowError('Needed %i bytes for number, but block size ' 'is %i' % (needed_bytes, block_size)) # Convert the number to bytes. - raw_bytes = [] while number > 0: raw_bytes.insert(0, byte(number & 0xFF)) number >>= 8 @@ -90,9 +92,9 @@ def _int2bytes(number, block_size=0): if block_size > 0: padding = (block_size - needed_bytes) * ZERO_BYTE else: - padding = b('') + padding = EMPTY_BYTE - return padding + b('').join(raw_bytes) + return padding + EMPTY_BYTE.join(raw_bytes) def bytes_leading(raw_bytes, needle=ZERO_BYTE): |