Completed
Pull Request — master (#138)
by
unknown
20:13
created

PKCS7Encoder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%
Metric Value
dl 0
loc 18
ccs 9
cts 14
cp 0.6429
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 8 2
A decode() 0 6 3
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from wechatpy.utils import to_binary, byte2int
4
5
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