Skip to content

Instantly share code, notes, and snippets.

@chris-wood
Created July 18, 2016 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-wood/87d0f34af234f36cdbf7f4630b4c09b0 to your computer and use it in GitHub Desktop.
Save chris-wood/87d0f34af234f36cdbf7f4630b4c09b0 to your computer and use it in GitHub Desktop.
def pad_pkcs7(x):
''' Pad the byte array according to the PKCS7 standard.
If the length is 15, pad with 0x1 = 16 - 15
If the length is 14, pad with 0x2 = 16 - 14
...
'''
length = len(x)
delta = 16 - length
result = x[:]
for i in range(length, 16):
result.append(delta)
assert len(result) == 16
return result
def is_valid_pad_pkcs7(x):
''' Check that the byte array has a valid padding.
The padding value must be a value between 1 and 16.
'''
pad_value = x[-1]
if pad_value == 0:
return False
for i in range(len(x) - 1, len(x) - pad_value - 1, -1):
if x[i] != pad_value:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment