def zip_folder_then_encrypt(self, password, should_remove): try: self.validate_entries(password) f_out_name = Path(str(self.path) + '.kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name.name + ' already exists') self.js_log_msg('Zipping...') zp = zip_dir(self.path) # Creates a temporary zip file target_size = os.stat(zp).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, zp, self.buffer_size) os.remove(zp) if should_remove: shutil.rmtree(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 setUp(self): self.ff = [] self.temp_dir_name = Path('temp-test-dir-u') os.mkdir(self.temp_dir_name) for i in range(5): temp_file_path = self.temp_dir_name / Path('temp_test_file_' + str(i + 1) + '.dat') with open(temp_file_path, 'wb') as f: f.write(b'abcdefghijklmnopqrstuvwxyz0123456789') # 36B self.ff.append(temp_file_path) self.zp = zip_dir(self.temp_dir_name) shutil.rmtree(self.temp_dir_name)
def zip_dir_then_encrypt(target_path: Path, should_remove: bool, buffer_size: int): f_out_name = str(target_path) + '.kpk' if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') password = get_password(ENCRYPT_MODE) print('\nZipping...') zp = zip_dir(target_path) # Creates a temporary zip file target_size = os.stat(zp).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) lib.encryptor.encrypt(key, salt, zp, buffer_size) os.remove(zp) if should_remove: shutil.rmtree(target_path)
def test_zip_dir(self): zp = zip_dir(self.temp_dir_name) self.assertTrue(zipfile.is_zipfile(zp)) with zipfile.ZipFile(zp, mode='r') as zf: self.assertListEqual([Path(f) for f in sorted(zf.namelist())], self.ff) os.remove(zp)
def execute(argv): if len(argv) == 2 or argv[2] == '-h' or argv[2] == '--help': print_help_encrypt() return should_remove = True if has_remove_flag(argv[2:]) else False should_zip = True if has_zip_flag(argv[2:]) else False target_path = get_path(argv[2:]) if target_path == None: raise Exception('PATH is not specified') if not os.path.exists(target_path): raise Exception('can not find ' + str(target_path)) if os.path.isfile(target_path): 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(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) encryptor = FileEncryptor(key, salt, target_path) encryptor.encrypt() if should_remove: os.remove(target_path) print('\r' + 20 * ' ' + '\r[■■■■■■■■■■] 100%\n' ) # TODO: This is a hack, should be fixed in lib/progress.py return if should_zip and os.path.isdir(target_path): f_out_name = str(target_path) + '.kpk' if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') password = get_password(ENCRYPT_MODE) print('\nZipping...') zp = zip_dir(target_path) # Creates a temporary zip file target_size = os.stat(zp).st_size if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) encryptor = FileEncryptor(key, salt, zp) encryptor.encrypt() os.remove(zp) if should_remove: shutil.rmtree(target_path) return if os.path.isdir(target_path): password = get_password(ENCRYPT_MODE) print('\nLooking for files in the directory...') ff = list_files(target_path, ENCRYPT_MODE) # List of files in the directory if len(ff) == 0: raise Exception(str(target_path) + ' is empty') target_size = calc_total_size(ff) if target_size == 0: raise Exception(str(target_path) + ' is empty') print('\nEncrypting...\n') progress = Progress() progress.set_total_size(target_size) key, salt = derive_key(password, None) for f in ff: f_out_name = replace_file_ext(f, 'kpk') if os.path.exists(f_out_name): # Overwrite error raise Exception(f_out_name + ' already exists') encryptor = FileEncryptor(key, salt, f) encryptor.encrypt() if should_remove: os.remove(f) print('\r' + 20 * ' ' + '\r[■■■■■■■■■■] 100%\n' ) # TODO: This is a hack, should be fixed in lib/progress.py