def decrypt_dir(target_path: Path, should_remove: bool, buffer_size: int): password = get_password(DECRYPT_MODE) print('\nDecrypting...\n') ff = list_files(target_path, DECRYPT_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') progress = Progress() progress.set_total_size(target_size) for f in ff: _, _ = lib.decryptor.decrypt(password, f, buffer_size) if should_remove: os.remove(f)
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 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 encrypt_dir(target_path: Path, should_remove: bool, buffer_size: int): 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(str(f_out_name) + ' already exists') lib.encryptor.encrypt(key, salt, f, buffer_size) if should_remove: os.remove(f)
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