def prune(self): parser = argparse.ArgumentParser( description='Delete all stored values') if not self.is_initialized(): print(colored("An initialization is needed.", Style.FAIL), file=sys.stderr) return 1 try: master = self.ask_masterpwd() res = input("Delete all your stored files ? [y/N]: ") if res.lower() != "y": print(colored("You decided to keep your files", Style.WARNING)) return 1 for file in os.listdir(local_data.appdir()): if (file[0] != "."): local_data.appfile(file).unlink() listFile = local_data.appfile(".list") if listFile.exists(): with open(listFile, "w"): pass print(colored("Your files are now deleted.", Style.OKGREEN)) except ChestException as e: print(colored(e.message, Style.FAIL), file=sys.stderr) return 1
def get_masterpwd_hash(self) -> bytes: filepath = local_data.appfile('.m', create=False) f = open(filepath, 'rb') h = f.read() f.close() return h
def store(name: str, value: str, master: bytes): filename = security.hash_str(name).hex() filepath = local_data.appfile(filename) data = pickle.dumps(value) data = security.encrypt(data, master) filepath.write_bytes(data) add_to_list(name, filename, master)
def delete(name: str, master: bytes): filename = security.hash_str(name).hex() filepath = local_data.appfile(filename, create=False) if filepath.exists(): data = filepath.read_bytes() try: data = security.decrypt(data, master) except InvalidTag: raise InvalidMasterPassword() filepath.unlink() else: raise ValueNotStored(name)
def get(name: str, master: bytes) -> str: filename = security.hash_str(name).hex() filepath = local_data.appfile(filename, create=False) if filepath.exists(): data = filepath.read_bytes() try: data = security.decrypt(data, master) except InvalidTag: raise InvalidMasterPassword() return pickle.loads(data) else: raise ValueNotStored(name)
def get_list(master: bytes, hashes: bool) -> str: filepath = local_data.appfile(".list") data = filepath.read_bytes() if len(data) > 0: try: data = security.decrypt(data, master) except InvalidTag: raise InvalidMasterPassword() data = pickle.loads(data) data = str(data, 'utf8') if not hashes: data = "\n".join(map(lambda s: s.split("=")[0], data.split("\n"))) return data
def init(self): masterpath = local_data.appfile('.m', create=False) if masterpath.exists(): print(colored("Initialization has already been done.", Style.FAIL), file=sys.stderr) else: master = getpass('Enter master password: '******'Re-enter master password: '******'t match.", Style.FAIL), file=sys.stderr) return 1 else: master_h = security.hash_str(master) masterpath.write_bytes(master_h)
def add_to_list(name: str, hashed: bytes, master: bytes): filepath = local_data.appfile(".list") if name in get_list(master, False).split('\n'): return data = filepath.read_bytes() if len(data) > 0: try: data = security.decrypt(data, master) except InvalidTag: raise InvalidMasterPassword() data = pickle.loads(data) data += bytes(f"{name}={hashed}\n", 'utf8') data = pickle.dumps(data) data = security.encrypt(data, master) filepath.write_bytes(data)
def is_initialized(self) -> bool: filepath = local_data.appfile('.m', create=False) return filepath.exists()