<commit_before>from cryptor import Cryptor
from M2Crypto.EVP import Cipher
import os
import logging


logger = logging.getLogger("remote.cryptors.m2cipher")


ENCODE = 1
DECODE = 0


class M2CipherCryptor(Cryptor):
    """Implementation of an AES based cryptor.

    """
    def __init__(self, key=None, iv=None, algorithm=None):
        """Instantiate an M2CipherCryptor object.

        key - The 128bit random cipher key.
        iv - The random initialization vector.
        algorithm - the M2Crypto algorithm to use.

        """
        self.key = key
        self.iv = iv
        self.algorithm = algorithm

        if self.key is None:
            self.key = os.random(16)

        if self.iv is None:
            self.iv = os.random(16)

        if self.algorithm is None:
            self.algorithm = 'aes_128_cbc'

    def encrypt(self, plaintext):
        """Encrypts plaintext with key and iv.

        Returns ciphertext as a string.

        """
        return self._cryptoOperation(plaintext, self.algorithm, self.key, self.iv, ENCODE)

    def decrypt(self, ciphertext):
        """Decrypts ciphertext with key and iv.

        Returns plaintext as a string.

        """
        return self._cryptoOperation(ciphertext, self.algorithm, self.key, self.iv, DECODE)

    @staticmethod
    def _cryptoOperation(data, algorithm, key, iv, operation):
        """Encrypts or decrypts data with the given key and initialization vector.

        This function performs an encryption if operation is ENCODE, or a
        decryption if operation is DECODE.

        """
        # Use M2Crypto's Cipher object. Odd work flow, but whatever.
        cipher = Cipher(alg=algorithm, key=key, iv=iv, op=operation)
        result = cipher.update(data)<commit_msg>Converted M2CipherCryptor to implement stream class.<commit_after>from cryptor import Cryptor
from M2Crypto.EVP import Cipher
import os
import logging


logger = logging.getLogger("remote.cryptors.m2cipher")


ENCODE = 1
DECODE = 0


class M2CipherCryptor(Cryptor):
    """Implementation of an AES based cryptor.

    """
    def __init__(self, targetStream, key=None, iv=None, algorithm=None, *args, **kwargs):
        """Instantiate an M2CipherCryptor object.

        key - The 128bit random cipher key.
        iv - The random initialization vector.
        algorithm - the M2Crypto algorithm to use.

        """
        self.key = key
        self.iv = iv
        self.algorithm = algorithm

        if self.key is None:
            self.key = os.random(16)

        if self.iv is None:
            self.iv = os.random(16)

        if self.algorithm is None:
            self.algorithm = 'aes_128_cbc'

        super(M2CipherCryptor, self).__init__(*args, **kwargs)

    def encrypt(self, plaintext):
        """Encrypts plaintext with key and iv.

        Returns ciphertext as a string.

        """
        return self._cryptoOperation(plaintext, self.algorithm, self.key, self.iv, ENCODE)

    def decrypt(self, ciphertext):
        """Decrypts ciphertext with key and iv.

        Returns plaintext as a string.

        """
        return self._cryptoOperation(ciphertext, self.algorithm, self.key, self.iv, DECODE)

    @staticmethod
    def _cryptoOperation(data, algorithm, key, iv, operation):
        """Encrypts or decrypts data with the given key and initialization vector.

        This function performs an encryption if operation is ENCODE, or a
        decryption if operation is DECODE.

        """
        # Use M2Crypto's Cipher object. Odd work flow, but whatever.
        cipher = Cipher(alg=algorithm, key=key, iv=iv, op=operation)
        result = cipher.update(data)
{'fraction_non_alphanumeric': 0.08765701802293828, 'fraction_numerical': 0.010376843255051884, 'mean_word_length': 2.4589235127478752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5702061', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1027, 'n_words': 342}