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

wechatpy.crypto.WeChatCipher   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A WeChatCipher.__init__() 0 6 1
A WeChatCipher.encrypt() 0 3 1
A WeChatCipher.decrypt() 0 3 1
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