diff --git a/set02/01_code.py b/set02/01_code.py deleted file mode 100644 index e5a0d9b..0000000 --- a/set02/01_code.py +++ /dev/null @@ -1 +0,0 @@ -#!/usr/bin/env python3 diff --git a/set02/09_code.py b/set02/09_code.py new file mode 100755 index 0000000..615a2eb --- /dev/null +++ b/set02/09_code.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +from common_set02 import pkcs7 + +def main(): + bts_unpadded = b'YELLOW SUBMARINE' + + bts_padded = pkcs7(bts_unpadded, 20) + + assert(bts_padded == b'YELLOW SUBMARINE\x04\x04\x04\x04') + + s = ( + 'bts_unpadded: \'{}\'\n' + 'bts_padded: \'{}\'\n' + ) + print(s.format(bts_unpadded, bts_padded), end='') + +if __name__ == "__main__": + main() diff --git a/set02/common_set02/__init__.py b/set02/common_set02/__init__.py new file mode 100644 index 0000000..8a8d42c --- /dev/null +++ b/set02/common_set02/__init__.py @@ -0,0 +1 @@ +from .utils import pkcs7 diff --git a/set02/common_set02/utils.py b/set02/common_set02/utils.py new file mode 100644 index 0000000..774c9dc --- /dev/null +++ b/set02/common_set02/utils.py @@ -0,0 +1,17 @@ +import math + +def pkcs7(bts, blk_size: int): + """ + bts: bytes to right pad + blk_size: int < 256 + + Right-pad bts until blk_size divides len(bts). + The byte value to pad with is the number bytes padded. + + returns: the padded bts + """ + if not blk_size < 256: + raise ValueError('need blk_size < 256') + + pad = abs(len(bts) - math.ceil(len(bts) / blk_size) * blk_size) + return bytes(pad if not i < len(bts) else bts[i] for i in range(len(bts)+pad))