from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
import hashlib


# SIMPLE CRYPT - requeres C compiler -------------------------------------------------------------------------

# def prepare_key(encryption_key):
#     if len(encryption_key) < 32:
#         encryption_key = (encryption_key * ((32 // len(encryption_key)) + 1))[:32]
#     return encryption_key

# def encrypt_data(data, encryption_key):
#     key = prepare_key(encryption_key)
#     encrypted_data = encrypt(key, data)
#     return encrypted_data

# def decrypt_data(data, encryption_key):
#     key = prepare_key(encryption_key)
#     decrypted_data = decrypt(key, data).decode('utf-8')
#     return decrypted_data

# pycryptodome -------------------------------------------------------------------------

def prepare_key(encryption_key):
    key = hashlib.sha256(encryption_key.encode()).digest()
    return key

def encrypt_data(data, encryption_key):
    key = prepare_key(encryption_key)
    cipher = AES.new(key, AES.MODE_CBC)
    ct_bytes = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
    iv = base64.b64encode(cipher.iv).decode('utf-8')
    ct = base64.b64encode(ct_bytes).decode('utf-8')
    return iv + ct

def decrypt_data(data, encryption_key):
    key = prepare_key(encryption_key)
    iv = base64.b64decode(data[:24])
    ct = base64.b64decode(data[24:])
    cipher = AES.new(key, AES.MODE_CBC, iv)
    pt = unpad(cipher.decrypt(ct), AES.block_size)
    return pt.decode('utf-8')