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

PKCS7Encoder.encode()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116
Metric Value
cc 2
dl 0
loc 8
ccs 6
cts 7
cp 0.8571
crap 2.0116
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