blob: d85fbe99604262f37f25c1ae65ebdebd27dff163 (
plain) (
blame)
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
|
"""
This module imports the Fernet symmetric encryption algorithm from the cryptography library.
It allows for secure encryption and decryption of data using a secret key.
"""
from cryptography.fernet import Fernet
VAULT_FILE = "vault.sqlite"
def generate_key() -> bytes:
"""Generates a new encryption key."""
return Fernet.generate_key()
def load_key(key_file: str) -> bytes:
"""
Loads an existing encryption key from the file.
Args:
key_file (str): Path to the key file.
"""
with open(key_file, "rb") as key:
return key.read()
def encrypt(key: bytes, filename: str = VAULT_FILE) -> None:
"""Encrypts the data in the specified file using the provided key."""
f = Fernet(key)
with open(filename, "rb") as vault:
data = vault.read()
encrypted_data = f.encrypt(data)
with open(filename, "wb") as vault:
vault.write(encrypted_data)
def decrypt(key: bytes, filename: str = VAULT_FILE) -> None:
"""Decrypts the data in the specified file using the provided key."""
f = Fernet(key)
with open(filename, "rb") as vault:
encrypted_data = vault.read()
decrypted_data = f.decrypt(encrypted_data)
with open(filename, "wb") as vault:
vault.write(decrypted_data)
|