wechatpy.crypto.pkcs7.PKCS7Encoder.decode()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
crap 3.072
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 10
        padding = byte2int(decrypted[-1])
21 10
        if padding < 1 or padding > 32:
22
            padding = 0
23
        return decrypted[:-padding]
24