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

WeChatCipher.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

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