Created
July 18, 2016 05:42
-
-
Save chris-wood/87d0f34af234f36cdbf7f4630b4c09b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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