aboutsummaryrefslogtreecommitdiff
path: root/crypto.py
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2024-11-02 16:58:31 -0500
committerChristian Cleberg <hello@cleberg.net>2024-11-02 16:58:31 -0500
commit598e71b5ea1392f25cca0290c5544ab1135c37a2 (patch)
tree76cd61b733e3bffae640e61146bc0c30c317c5cc /crypto.py
parent0bef69a049f4bace9b06cb4beb3b0505dd7b7a44 (diff)
downloadyoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.tar.gz
yoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.tar.bz2
yoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.zip
add pylint workflow
Diffstat (limited to 'crypto.py')
-rw-r--r--crypto.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/crypto.py b/crypto.py
index a96642b..9b0a423 100644
--- a/crypto.py
+++ b/crypto.py
@@ -1,30 +1,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'
+VAULT_FILE = 'vault.sqlite'
def generate_key() -> bytes:
- new_key = Fernet.generate_key()
- return new_key
+ """Generates a new encryption key."""
+ return Fernet.generate_key()
def load_key(key_file: str) -> bytes:
- return open(key_file, 'rb').read()
+ """
+ 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, filename=vault_file) -> None:
+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 file:
- file_data = file.read()
- encrypted_data = f.encrypt(file_data)
- with open(filename, 'wb') as file:
- file.write(encrypted_data)
+ 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, filename=vault_file) -> None:
+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 file:
- encrypted_data = file.read()
+ with open(filename, 'rb') as vault:
+ encrypted_data = vault.read()
decrypted_data = f.decrypt(encrypted_data)
- with open(filename, 'wb') as file:
- file.write(decrypted_data)
+ with open(filename, 'wb') as vault:
+ vault.write(decrypted_data)