def load_database_file(self): Paths.create_paths() try: self.DEVICES = pickle.load(open(Paths.DATABASE_FILE, "rb")) except FileNotFoundError: # Create an empty database file if non exists pickle.dump([], open(Paths.DATABASE_FILE, "wb")) self.load_database_file()
def load_log_filename(self): Paths.create_paths() try: with open(Paths.LOG_PATHNAME_FILE, "rb") as f: log_file = f.read().decode("UTF-8") f.close() return log_file except FileNotFoundError: return None
def read_password_hash(self): Paths.create_paths() try: with open(Paths.PASSWORD_FILE, "rb") as f: password_hash = f.read() f.close() return password_hash except FileNotFoundError: if self.LOGGER is not None: self.LOGGER.log("Password file doesn't exist") return None
def update_password_hash(self, cleartext_password): # Generate the hash salt_handler = SaltHandler() password_handler = PasswordHandler(salt_handler, cleartext_password) key = password_handler.generate() # Write the hash to the file Paths.create_paths() with open(Paths.PASSWORD_FILE, "wb") as f: f.write(key) f.close() # Log feedback if self.LOGGER is not None: self.LOGGER.log("Password was updated")
def __init__(self, quiet): from os.path import isfile # Get a name for the log file log_file = self.load_log_filename() if log_file is None: # If no name is stored on the disk, generate a new one. log_file = Paths.LOG_DIR + self.generate_log_name() # Ensure that log file exists if not isfile(log_file): Paths.create_paths() open(log_file, "wb").close() # Initialize empty file self.LOG_FILE = log_file self.dump_log_filename() self.QUIET = quiet
def dump_salt(self): Paths.create_paths() with open(Paths.SALT_FILE, "wb") as f: f.write(self.SALT) f.close()
def dump_log_filename(self): Paths.create_paths() with open(Paths.LOG_PATHNAME_FILE, "wb") as f: f.write(self.LOG_FILE.encode("UTF-8")) f.close()