-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .utils import pkcs7 |
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
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)) |