Completed
Push — master ( e6b198...e30f90 )
by Messense
11:09 queued 10:06
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 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