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

WeChatCipher.encrypt()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
cc 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
4 6
from cryptography.hazmat.backends import default_backend
5
6
7 6
class WeChatCipher(object):
8
9 6
    def __init__(self, key):
10 6
        backend = default_backend()
11 6
        self.cipher = Cipher(
12
            algorithms.AES(key),
13
            modes.CBC(key[:16]),
14
            backend=backend
15
        )
16
17 6
    def encrypt(self, plaintext):
18 6
        encryptor = self.cipher.encryptor()
19 6
        return encryptor.update(plaintext) + encryptor.finalize()
20
21 6
    def decrypt(self, ciphertext):
22 6
        decryptor = self.cipher.decryptor()
23
        return decryptor.update(ciphertext) + decryptor.finalize()
24