forked from aajmorgan6/FileEncrypter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
72 lines (55 loc) · 2.09 KB
/
Copy pathencrypt.py
File metadata and controls
72 lines (55 loc) · 2.09 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import cryptography
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
import os
import base64
import pathlib
import keyring
def read_secret_string(filename: str) -> str:
"""Returns the secret string."""
pw = keyring.get_password(filename, "FileEncrypter")
return pw if pw else "" # make sure to return str for type hint
def write_secret_string(filename: str, secret_string: str) -> bool:
"""Writes the secret string."""
try:
keyring.set_password(filename, "FileEncrypter", secret_string)
return True
except keyring.errors.PasswordSetError as e:
print(f" The attempted write to the login keychain failed.")
return False
def delete_keychain(filename: str) -> bool:
try:
keyring.delete_password(filename, "FileEncrypter")
return True
except keyring.errors.PasswordDeleteError as e:
print(" The attempted delete to the login keychain failed.")
return False
def make_key(salt: bytes, password: str) -> bytes:
kdf = Scrypt(salt=salt, length=32, n=2**14, r=8, p=1)
derived = kdf.derive(password.encode())
return base64.urlsafe_b64encode(derived)
def encrypt_file_algo(encrypt_file: str, key: bytes) -> None:
'''
Key will come from both password and seed
'''
f = Fernet(key)
with open(encrypt_file, "rb") as infile:
to_encrypt = infile.read()
encrypted = f.encrypt(to_encrypt)
with open(encrypt_file+".box", "wb") as outfile:
outfile.write(encrypted)
# TODO: delete old file?, or just overwrite
os.remove(pathlib.Path(encrypt_file))
def decrypt_file_algo(decrypt_file: str, key) -> bool:
f = Fernet(key)
with open(decrypt_file, "rb") as infile:
to_decrypt = infile.read()
try:
decrypted = f.decrypt(to_decrypt)
except cryptography.fernet.InvalidToken:
print("Invalid token")
return False
with open(decrypt_file[:-len('.box')], "wb") as outfile:
outfile.write(decrypted)
os.remove(pathlib.Path(decrypt_file))
return True