Total Complexity | 5 |
Total Lines | 18 |
Duplicated Lines | 0 % |
Coverage | 64.29% |
1 | # -*- coding: utf-8 -*- |
||
6 | 10 | class PKCS7Encoder(object): |
|
7 | 10 | block_size = 32 |
|
8 | |||
9 | 10 | @classmethod |
|
10 | def encode(cls, text): |
||
11 | 10 | length = len(text) |
|
12 | 10 | padding_count = cls.block_size - length % cls.block_size |
|
13 | 10 | if padding_count == 0: |
|
14 | padding_count = cls.block_size |
||
15 | 10 | padding = to_binary(chr(padding_count)) |
|
16 | 10 | return text + padding * padding_count |
|
17 | |||
18 | 10 | @classmethod |
|
19 | def decode(cls, decrypted): |
||
20 | padding = byte2int(decrypted[-1]) |
||
21 | if padding < 1 or padding > 32: |
||
22 | padding = 0 |
||
23 | return decrypted[:-padding] |
||
24 |