krz/yoshi-cli
A password manager for the command line.
clone: git clone https://gitbay.org/krz/yoshi-cli.git
1"""
2This module imports the Fernet symmetric encryption algorithm from the cryptography library.
3
4It allows for secure encryption and decryption of data using a secret key.
5"""
6
7from cryptography.fernet import Fernet
8
9VAULT_FILE = "vault.sqlite"
10
11
12def generate_key() -> bytes:
13 """Generates a new encryption key."""
14 return Fernet.generate_key()
15
16
17def load_key(key_file: str) -> bytes:
18 """
19 Loads an existing encryption key from the file.
20
21 Args:
22 key_file (str): Path to the key file.
23 """
24 with open(key_file, "rb") as key:
25 return key.read()
26
27
28def encrypt(key: bytes, filename: str = VAULT_FILE) -> None:
29 """Encrypts the data in the specified file using the provided key."""
30 f = Fernet(key)
31 with open(filename, "rb") as vault:
32 data = vault.read()
33 encrypted_data = f.encrypt(data)
34 with open(filename, "wb") as vault:
35 vault.write(encrypted_data)
36
37
38def decrypt(key: bytes, filename: str = VAULT_FILE) -> None:
39 """Decrypts the data in the specified file using the provided key."""
40 f = Fernet(key)
41 with open(filename, "rb") as vault:
42 encrypted_data = vault.read()
43 decrypted_data = f.decrypt(encrypted_data)
44 with open(filename, "wb") as vault:
45 vault.write(decrypted_data)