# encrypts/decrypts them separately. See https://stackoverflow.com/a/71068357.
def encrypt_chunks_in_place(encryption_key, path):
block = 1 << 16
+
+ file_size = os.path.getsize(path)
+ processed_bytes = 0
+ percentage = 0
+
tmp_path = path + str(uuid.uuid4())
with open(path, 'rb') as input_file, open(tmp_path, 'wb') as output_file:
while True:
bytes_as_int = len(encrypted_bytes).to_bytes(4, 'big')
output_file.write(bytes_as_int)
output_file.write(encrypted_bytes)
+
+ processed_bytes = processed_bytes + len(unencrypted_bytes)
+ new_percentage = int(processed_bytes / file_size * 100)
+ if new_percentage > percentage + 7:
+ percentage = new_percentage
+ print(f'Encrypt: {percentage}%')
+
if len(unencrypted_bytes) < block:
break
os.rename(tmp_path, path)
def decrypt_chunks_in_place(encryption_key, path):
+ file_size = os.path.getsize(path)
+ processed_bytes = 0
+ percentage = 0
+
tmp_path = path + str(uuid.uuid4())
with open(path, 'rb') as input_file, open(tmp_path, 'wb') as output_file:
while True:
chunk = input_file.read(bytes_as_int)
decrypted_bytes = encryption_key.decrypt(chunk)
output_file.write(decrypted_bytes)
+
+ processed_bytes = processed_bytes + 4 + len(chunk)
+ new_percentage = int(processed_bytes / file_size * 100)
+ if new_percentage > percentage + 7:
+ percentage = new_percentage
+ print(f'Decrypt: {percentage}%')
+
os.rename(tmp_path, path)
class auth_server():
done = False
while done is False:
status, done = downloader.next_chunk()
- print(f'Download {int(status.progress() * 100)}.')
+ print(f'Download: {int(status.progress() * 100)}%.')
decrypt_chunks_in_place(args.key, path_in_tmp)
print(f'{args.path} decrypted.')