# This Python file uses the following encoding: utf-8
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
import platform
from Crypto.Hash import SHA256, SHA3_512
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Random import get_random_bytes
import random
import string


class Vermasseln:
    def oscarVermasseln(self, data, local= True):
        b_data = data.encode("utf-8")
        cipher = self.__vermasslungsKobold(local)

        ciphertext, tag = cipher.encrypt_and_digest(b_data)
        decoded_data = [b64encode(x).decode("utf-8") for x in (ciphertext, tag)]
        storable_data = ".".join(decoded_data)

        return storable_data

    def entschluesseln(self, data, local= True):
        try:
            data_list = data.split(".")
            encoded_data = [b64decode(x) for x in data_list]
            cipher = self.__vermasslungsKobold(local)
            decrypted_data = cipher.decrypt_and_verify(encoded_data[0], encoded_data[1])
            decrypted_data = decrypted_data.decode("utf-8")
        except (ValueError, IndexError) as e:
            print(f"Configuration corrupted: {str(e)}")
            decrypted_data = None

        except Exception as e:
            print(str(e))
            decrypted_data = None

        return decrypted_data

    def __vermasslungsKobold(self, local= True):
        key = platform.processor().encode("utf-8") if local else b"(==daniishtverhaftetwegensexy#)"
        key = key[0:31]
        hash_key = SHA256.new(key)
        hashed = hash_key.digest()
        nonce = platform.machine().encode("utf-8") if local else b"(==Uskarishtverhaftetwegensexy#)"
        cipher = AES.new(hashed, AES.MODE_SIV, nonce = nonce)
        return cipher

    @classmethod
    def userPasswordHash(self, password, salt = None):
        if not salt:
            salt = "".join(random.choice(string.ascii_letters + string.digits) for i in range (32))
        hash_pw = (salt + password).encode("utf-8")
        h_obj = SHA3_512.new(hash_pw)
        password = salt + "$" + h_obj.hexdigest()

        return password







