Skip to content

Commit

Permalink
Reduce byte pad stripping complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
nnevala committed Jan 29, 2015
1 parent ddc7baa commit 5dccef1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions blimey/agile_keychain/_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def byte_pad(input_bytes, length=16):


def strip_byte_padding(input_bytes, length=16):
if fmod(len(input_bytes), length) != 0:
raise ValueError("Input byte length is not divisible by %s " % length)
_assert_bytes_length_divisible_by(input_bytes, length)

if input_bytes == b'':
return input_bytes
Expand All @@ -167,7 +166,16 @@ def strip_byte_padding(input_bytes, length=16):
if last_byte_value > length:
return input_bytes

if list(set(input_bytes[-last_byte_value:])) == [last_byte_value]:
return input_bytes[0:-last_byte_value]
_assert_last_byte_value_indicates_padding_size(input_bytes, last_byte_value)

return input_bytes[0:-last_byte_value]


def _assert_bytes_length_divisible_by(input_bytes, length):
if fmod(len(input_bytes), length) != 0:
raise ValueError("Input byte length is not divisible by %s " % length)


raise ValueError("Invalid padding")
def _assert_last_byte_value_indicates_padding_size(input_bytes, byte_value):
if list(set(input_bytes[-byte_value:])) != [byte_value]:
raise ValueError("Invalid padding")

0 comments on commit 5dccef1

Please sign in to comment.