-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadding.py
More file actions
27 lines (23 loc) · 875 Bytes
/
padding.py
File metadata and controls
27 lines (23 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class PaddingError(Exception):
pass
#Pads a block (string) to the given block length using PKCS#7
#Assumes that we will never be asked to add more than 255 bytes of padding
def pkcs7(block, blocklen):
numPadding = blocklen - len(block)
if numPadding <= 0:
return block
else:
return block+chr(numPadding)*numPadding
#Adds the smallest amount of valid padding such that the string is a multiple of the block length
def pkcs7String(string, blocklen):
if len(string) % blocklen == 0:
return pkcs7(string, len(string)+blocklen)
else:
paddedLen = (len(string)/blocklen + 1)*blocklen
return pkcs7(string, paddedLen)
#Removes pkcs7 padding from a string. Raises a PaddingError if padding is not valid.
def stripPkcs7(text):
numBytes = ord(text[-1])
if text[-numBytes:] != chr(numBytes)*numBytes:
raise PaddingError("Invalid padding")
return text[:-numBytes]