def test_file_ext(self): p1 = Path('path/to/file.txt') self.assertEqual(file_ext(p1), 'txt') p2 = Path('path/to/file') self.assertEqual(file_ext(p2), '') p3 = Path('path/to/file.tar.gz') self.assertEqual(file_ext(p3), 'gz')
def encrypt_file(self, password, should_remove): try: self.validate_entries(password) if file_ext(self.path) == 'kpk': raise Exception('can not encrypt ' + self.path.name) f_out_name = replace_file_ext(self.path, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name.name + ' already exists') target_size = os.stat(self.path).st_size if target_size == 0: raise Exception(self.path.name + ' is empty') self.progress.set_total_size(target_size) self.js_log_msg('Encrypting...') key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, self.path, self.buffer_size) if should_remove: os.remove(self.path) self.path = Path('') self.js_reset_encrypt_form() self.js_log_msg('Done') except Exception as err: self.js_clear_logs() self.js_log_error(err.args[0]) finally: self.progress.reset() self.js_enable_form()
def decrypt_file(self, password, should_remove): try: self.validate_entries(password) if file_ext(self.path) != 'kpk': raise Exception('can not decrypt ' + self.path.name) target_size = os.stat(self.path).st_size if target_size == 0: raise Exception(self.path.name + ' is empty') self.progress.set_total_size(target_size) self.js_log_msg('Decrypting...') f_out_path, f_out_ext = lib.decryptor.decrypt( password, self.path, self.buffer_size) if f_out_ext == TEMP_ZIP_EXT: unzip_dir(f_out_path) os.remove(f_out_path) if should_remove: os.remove(self.path) self.path = Path('') self.js_reset_decrypt_form() self.js_log_msg('Done') except Exception as err: self.js_clear_logs() self.js_log_error(err.args[0]) finally: self.progress.reset() self.js_enable_form()
def decrypt_file(target_path: Path, should_remove: bool, buffer_size: int): if file_ext(target_path) != 'kpk': raise Exception('can not decrypt ' + str(target_path)) target_size = os.stat(target_path).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') password = get_password(DECRYPT_MODE) print('\nDecrypting...\n') progress = Progress() progress.set_total_size(target_size) f_out_path, f_out_ext = lib.decryptor.decrypt(password, target_path, buffer_size) if f_out_ext == TEMP_ZIP_EXT: unzip_dir(f_out_path) os.remove(f_out_path) if should_remove: os.remove(target_path)
def encrypt_file(target_path: Path, should_remove: bool, buffer_size: int): if file_ext(target_path) == 'kpk': raise Exception('can not encrypt ' + str(target_path)) f_out_name = replace_file_ext(target_path, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(str(f_out_name) + ' already exists') target_size = os.stat(target_path).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') password = get_password(ENCRYPT_MODE) print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, target_path, buffer_size) if should_remove: os.remove(target_path)
def encrypt(key: bytes, salt: bytes, f_in_path: Path, buffer_size: int): ext = file_ext(f_in_path) if len(ext) > 11: raise Exception( 'unable to encrypt files with extension longer than 11B') iv = urandom(16) encryptor = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()).encryptor() header = { 'iv': iv, 'salt': salt, 'cipher_ext': encryptor.update(_pad_ext(bytes(ext, 'utf-8'))) } f_out_path = replace_file_ext(f_in_path, 'kpk') pipeline = new_pipeline(buffer_size) with open(f_in_path, 'rb') as fd_in: with open(f_out_path, 'wb') as fd_out: _write_header(fd_out, header) pipeline(fd_in, _pad_bytes, encryptor.update, fd_out) fd_out.write(encryptor.finalize())
def list_files(dir_path: Path, mode: int) -> [Path]: if mode == DECRYPT_MODE: return list(dir_path.rglob('*.kpk')) return list( filter(lambda f: os.path.isfile(f) and file_ext(f) != 'kpk', list(dir_path.rglob('*'))))