forked from kawaiiDango/telegram-delete-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_encrypt.py
35 lines (29 loc) · 928 Bytes
/
file_encrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import io
from os import stat
from contextlib import contextmanager
import pyAesCrypt
import config
BUFFER_SIZE = 1024 * 1024
# this is meant to be more about obfuscation and less about security
@contextmanager
def encrypted(file_path, password=config.FILE_PASSWORD):
tmp_file = io.BytesIO()
try:
yield tmp_file
finally:
tmp_file.seek(0)
with open(file_path, 'wb') as f_out:
pyAesCrypt.encryptStream(
tmp_file, f_out, password, bufferSize=BUFFER_SIZE)
tmp_file.close()
@contextmanager
def decrypted(file_path, password=config.FILE_PASSWORD):
tmp_file = io.BytesIO()
try:
with open(file_path, 'rb') as f_in:
pyAesCrypt.decryptStream(
f_in, tmp_file, password, bufferSize=BUFFER_SIZE, inputLength=stat(file_path).st_size)
tmp_file.seek(0)
yield tmp_file
finally:
tmp_file.close()