def save_portfolio_object(portfolio_obj): # First, set this object into the portfolio dict, for the first time it's saved: id_number = portfolio_obj.id_number config.PORTFOLIO_OBJECTS_DICT[str(id_number)] = portfolio_obj encryption_possible = False print line_number(), "config.ENCRYPTION_POSSIBLE", config.ENCRYPTION_POSSIBLE if config.ENCRYPTION_POSSIBLE: try: import Crypto from modules.simplecrypt import encrypt, decrypt encryption_possible = True except Exception as e: pass if encryption_possible: path = portfolio_account_obj_file_path % (id_number, "txt") unencrypted_pickle_string = pickle.dumps(portfolio_obj) encrypted_string = encrypt(config.PASSWORD, unencrypted_pickle_string) with open(path, "w") as output: output.write(encrypted_string) else: path = portfolio_account_obj_file_path % (id_number, "pk") with open(path, "w") as output: pickle.dump(portfolio_obj, output, pickle.HIGHEST_PROTOCOL)
def reset_all_encrypted_files_with_new_password(old_password, new_password, encryption_strength): old_password_hashed = hashlib.sha256(old_password).hexdigest() new_password_hashed = hashlib.sha256(new_password).hexdigest() file_names = os.listdir(do_not_copy_path) file_names.remove(password_file_name) from modules.simplecrypt import encrypt, decrypt for file_name in file_names: path = do_not_copy_path + "/" + file_name encrypted_file = open(path, "r") encrypted_string = encrypted_file.read() encrypted_file.close() try: decrypted_string = decrypt(old_password_hashed, encrypted_string) re_encrypted_string = encrypt(new_password_hashed, decrypted_string) with open(path, "w") as output: output.write(re_encrypted_string) print line_number(), file_name, "has been encrypted and saved" except Exception as e: print e print line_number(), "Error:", file_name, "did not save properly, and the data will need to be retrieved manually with your old password." config.PASSWORD = new_password_hashed if encryption_strength: config.ENCRYPTION_HARDNESS_LEVEL = encryption_strength save_encryption_strength(encryption_strength) save_password(new_password) print line_number(), "You have successfully changed your password."
def reset_all_encrypted_files_with_new_password(old_password, new_password, encryption_strength): old_password_hashed = hashlib.sha256(old_password).hexdigest() new_password_hashed = hashlib.sha256(new_password).hexdigest() file_names = os.listdir(do_not_copy_path) file_names.remove(password_file_name) from modules.simplecrypt import encrypt, decrypt for file_name in file_names: path = do_not_copy_path + "/" + file_name encrypted_file = open(path, 'r') encrypted_string = encrypted_file.read() encrypted_file.close() try: decrypted_string = decrypt(old_password_hashed, encrypted_string) re_encrypted_string = encrypt(new_password_hashed, decrypted_string) with open(path, 'w') as output: output.write(re_encrypted_string) logging.info("{} has been encrypted and saved".format(file_name)) except Exception as e: logging.error(e) logging.error( "Error: {} did not save properly, and the data will need to be retrieved manually with your old password." .format(file_name)) config.PASSWORD = new_password_hashed if encryption_strength: config.ENCRYPTION_HARDNESS_LEVEL = encryption_strength save_encryption_strength(encryption_strength) save_password(new_password) logging.info("You have successfully changed your password.")
def save_portfolio_object(portfolio_obj): # First, set this object into the portfolio dict, for the first time it's saved: id_number = portfolio_obj.id_number config.PORTFOLIO_OBJECTS_DICT[str(id_number)] = portfolio_obj encryption_possible = False print line_number( ), "config.ENCRYPTION_POSSIBLE", config.ENCRYPTION_POSSIBLE if config.ENCRYPTION_POSSIBLE: try: import Crypto from modules.simplecrypt import encrypt, decrypt encryption_possible = True except Exception as e: pass if encryption_possible: path = portfolio_account_obj_file_path % (id_number, "txt") unencrypted_pickle_string = pickle.dumps(portfolio_obj) encrypted_string = encrypt(config.PASSWORD, unencrypted_pickle_string) with open(path, 'w') as output: output.write(encrypted_string) else: path = portfolio_account_obj_file_path % (id_number, "pk") with open(path, 'w') as output: pickle.dump(portfolio_obj, output, pickle.HIGHEST_PROTOCOL)
def encrypt_file(): a = TestObj() b = pickle.dumps(a) print type(b) print b c = encrypt(password, b) print type(c) print c with open(test_path, 'w') as output: output.write(c)
def save_DATA_ABOUT_PORTFOLIOS(password=""): data = config.DATA_ABOUT_PORTFOLIOS if config.ENCRYPTION_POSSIBLE: try: import Crypto from modules.simplecrypt import encrypt, decrypt except: config.ENCRYPTION_POSSIBLE = False print line_number(), "Error: DATA_ABOUT_PORTFOLIOS did not save" return unencrypted_pickle_string = pickle.dumps(data) encrypted_string = encrypt(password, unencrypted_pickle_string) print line_number(), "Saving encrypted DATA_ABOUT_PORTFOLIOS." with open(portfolios_path % "txt", 'w') as output: output.write(encrypted_string) else: print line_number(), "Saving unencrypted DATA_ABOUT_PORTFOLIOS." with open(portfolios_path % "pk", 'w') as output: pickle.dump(data, output, pickle.HIGHEST_PROTOCOL)
def save_portfolio_object(portfolio_data, id_number, password=hashlib.sha256("").hexdigest()): encryption_possible = False if config.ENCRYPTION_POSSIBLE: try: import Crypto from modules.simplecrypt import encrypt, decrypt encryption_possible = True config.ENCRYPTION_POSSIBLE = True except Exception as e: pass if encryption_possible: path = portfolio_account_obj_file_path % (id_number, "txt") unencrypted_pickle_string = pickle.dumps(portfolio_data) encrypted_string = encrypt(password, unencrypted_pickle_string) with open(path, 'w') as output: output.write(encrypted_string, output) else: path = portfolio_account_obj_file_path % (id_number, "pk") with open(path, 'w') as output: pickle.dump(portfolio_data, output, pickle.HIGHEST_PROTOCOL)
def save_DATA_ABOUT_PORTFOLIOS(): data = config.DATA_ABOUT_PORTFOLIOS print line_number( ), "config.ENCRYPTION_POSSIBLE", config.ENCRYPTION_POSSIBLE if config.ENCRYPTION_POSSIBLE: try: import Crypto from modules.simplecrypt import encrypt, decrypt except: config.ENCRYPTION_POSSIBLE = False print line_number(), "Error: DATA_ABOUT_PORTFOLIOS did not save" return unencrypted_pickle_string = pickle.dumps(data) encrypted_string = encrypt(config.PASSWORD, unencrypted_pickle_string) print line_number(), "Saving encrypted DATA_ABOUT_PORTFOLIOS." with open(portfolios_path % "txt", 'w') as output: output.write(encrypted_string) else: print line_number(), "Saving unencrypted DATA_ABOUT_PORTFOLIOS." with open(portfolios_path % "pk", 'w') as output: pickle.dump(data, output, pickle.HIGHEST_PROTOCOL)