Completed
Push — master ( e6b198...e30f90 )
by Messense
11:09 queued 10:06
created

PKCS7Encoder.decode()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 7.608
Metric Value
cc 3
dl 0
loc 6
ccs 1
cts 5
cp 0.2
crap 7.608
rs 9.4285
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