<commit_before>"""
Created on 16 Oct 2020

@author: <NAME> (<EMAIL>)

requires pycryptodome

https://stackoverflow.com/questions/42568262/how-to-encrypt-text-with-a-password-in-python
"""

import base64
import sys

from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256

from getpass import getpass


# --------------------------------------------------------------------------------------------------------------------

class Crypt(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def key_from_user():
        print("Enter password for encryption: ", file=sys.stderr)
        return getpass()


    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def encrypt(key, plain_text):
        key_bytes = key.encode()
        source = plain_text.encode()

        key = SHA256.new(key_bytes).digest()                # use SHA-256 over our key to get a proper-sized AES key
        iv = Random.new().read(AES.block_size)              # generate IV
        encryptor = AES.new(key, AES.MODE_CBC, iv)

        padding = AES.block_size - len(source) % AES.block_size  # calculate needed padding
        source += bytes([padding]) * padding
        data = iv + encryptor.encrypt(source)               # store the IV at the beginning and encrypt

        return base64.b64encode(data).decode()


    @staticmethod
    def decrypt(key, cypher_text):
        key_bytes = key.encode()
        source = base64.b64decode(cypher_text.encode())

        key = SHA256.new(key_bytes).digest()                # use SHA-256 over our key to get a proper-sized AES key<commit_msg>Cleaning<commit_after>"""
Created on 16 Oct 2020

@author: <NAME> (<EMAIL>)

requires pycryptodome

https://stackoverflow.com/questions/42568262/how-to-encrypt-text-with-a-password-in-python
"""

import base64
import sys

from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256

from getpass import getpass


# --------------------------------------------------------------------------------------------------------------------

class Crypt(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def key_from_user():
        print("Enter password for encryption: ", file=sys.stderr)
        return getpass()


    # ----------------------------------------------------------------------------------------------------------------

    @staticmethod
    def encrypt(key, plain_text):
        key_bytes = key.encode()
        source = plain_text.encode()

        key = SHA256.new(key_bytes).digest()                # use SHA-256 over our key to get a proper-sized AES key
        iv = Random.new().read(AES.block_size)              # generate IV
        encryptor = AES.new(key, AES.MODE_CBC, iv)

        padding = AES.block_size - len(source) % AES.block_size     # calculate needed padding
        source += bytes([padding]) * padding
        data = iv + encryptor.encrypt(source)               # store the IV at the beginning and encrypt

        return base64.b64encode(data).decode()


    @staticmethod
    def decrypt(key, cypher_text):
        key_bytes = key.encode()
        source = base64.b64decode(cypher_text.encode())

        key = SHA256.new(key_bytes).digest()                # use SHA-256 over our key to get a proper-sized AES key
{'fraction_non_alphanumeric': 0.2721238938053097, 'fraction_numerical': 0.02157079646017699, 'mean_word_length': 1.3671465968586387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '4482069', 'n_tokens_mistral': 992, 'n_tokens_neox': 869, 'n_words': 235}