# Importing Fernet class
from cryptography.fernet import Fernet
# Importing dump and load function
from pickle import dump,load

# To generate a strong pw
def generate_pw():
    from random import choice
    choices = list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=.,/<>?;:\\|[]}{")
    pw = ""
    for i in range(25):
        pw += choice(choices)
    return pw
    del pw,choice

# To get master pw from the file
def get_masterpw():
    # Opening the file storing master pw
    with open("key.key",'rb') as file:
        # Loading data
        keys = load(file)
        # Master pw is converted from bytes to string
        key = keys[0].decode()
        del keys
    # Return keys
    return key

# To get key from the file
def get_key():
    # Opening the file storing master pw
    with open("key.key",'rb') as file:
        # Loading data
        keys = load(file)
        # Key is converted from bytes to string
        key = keys[1].decode()
        del keys
    # Return keys
    return key

# To store master pw in the file
def add_keys(masterpw,key):
    # Opening the file to store master pw
    with  open("key.key",'wb') as file:
        # Making list of value to upload
        # key is already in bytes # Converting to bytes is not necessary
        keys = [masterpw.encode(),key]
        # Dumping the master pw to file
        dump(keys,file)
        # Deleting the variable
        del masterpw,key,keys

# Checking if user is running program for first time
def is_1st_time():
    # Trying to open bytes file
    # If file is opened means program was executed once or more
    try:
        with open("key.key",'rb') as file:
            pass
        return False
    # FileNotFound means its first time
    # Or either its not in directory of this file or user deleted it :) #
    except FileNotFoundError:
        return True

# Function to copy pw to clipboard
def copy2clip(pw):
    # Importing copy function
    from pyperclip import copy
    # Copying pw to clipboard
    copy(pw)
    del pw,copy

# Encrypting the text
def encrypt(text, key):
    try:
        # Defining Fernet(class) using the key
        fernet = Fernet(key)
        # Encryption # Text is converted to bytes
        encrypted_text = fernet.encrypt(text.encode())
        del key
        # Return encrypted text
        return encrypted_text
    # Error message if any
    except Exception as e:
        print(f"Error occured:{e}\nProcess failed!")

# Decrypting the text
def decrypt(text, key):
    try:
        # Defining Fernet(class) using the key
        fernet = Fernet(key)
        # Decryption # Text is converted from bytes to string
        decrypted_text = fernet.decrypt(text).decode()
        del key
        # Return decrypted text
        return decrypted_text
    # Error message if any
    except Exception as e:
        print(f"Error occured:{e}\nProcess failed!")
[((70, 4, 70, 12), 'pyperclip.copy', 'copy', ({(70, 9, 70, 11): 'pw'}, {}), '(pw)', False, 'from pyperclip import copy\n'), ((12, 14, 12, 29), 'random.choice', 'choice', ({(12, 21, 12, 28): 'choices'}, {}), '(choices)', False, 'from random import choice\n'), ((21, 15, 21, 25), 'pickle.load', 'load', ({(21, 20, 21, 24): 'file'}, {}), '(file)', False, 'from pickle import dump, load\n'), ((33, 15, 33, 25), 'pickle.load', 'load', ({(33, 20, 33, 24): 'file'}, {}), '(file)', False, 'from pickle import dump, load\n'), ((48, 8, 48, 23), 'pickle.dump', 'dump', ({(48, 13, 48, 17): 'keys', (48, 18, 48, 22): 'file'}, {}), '(keys, file)', False, 'from pickle import dump, load\n'), ((77, 17, 77, 28), 'cryptography.fernet.Fernet', 'Fernet', ({(77, 24, 77, 27): 'key'}, {}), '(key)', False, 'from cryptography.fernet import Fernet\n'), ((91, 17, 91, 28), 'cryptography.fernet.Fernet', 'Fernet', ({(91, 24, 91, 27): 'key'}, {}), '(key)', False, 'from cryptography.fernet import Fernet\n')]