Skip to content

Commit

Permalink
complete set02/09
Browse files Browse the repository at this point in the history
  • Loading branch information
stumash committed Jul 4, 2018
1 parent 18b4171 commit aa56279
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 0 additions & 1 deletion set02/01_code.py

This file was deleted.

19 changes: 19 additions & 0 deletions set02/09_code.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions set02/common_set02/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import pkcs7
17 changes: 17 additions & 0 deletions set02/common_set02/utils.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit aa56279

Please sign in to comment.