summaryrefslogtreecommitdiff
path: root/yoshi/crypto.py
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2024-11-06 23:23:27 -0600
committerChristian Cleberg <hello@cleberg.net>2024-11-06 23:23:27 -0600
commit6dde4dd0bc5e5f91f89587c75a30c9ef7a24494c (patch)
tree6cf4b78ddd63a4606e19fcad423ed2e19ad2a268 /yoshi/crypto.py
parentb5a5fadff88615c8da8a9feb80c86fd8adb238f5 (diff)
downloadyoshi-cli-6dde4dd0bc5e5f91f89587c75a30c9ef7a24494c.tar.gz
yoshi-cli-6dde4dd0bc5e5f91f89587c75a30c9ef7a24494c.tar.bz2
yoshi-cli-6dde4dd0bc5e5f91f89587c75a30c9ef7a24494c.zip
package as a cli app
Diffstat (limited to 'yoshi/crypto.py')
-rw-r--r--yoshi/crypto.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/yoshi/crypto.py b/yoshi/crypto.py
new file mode 100644
index 0000000..9b0a423
--- /dev/null
+++ b/yoshi/crypto.py
@@ -0,0 +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'
+
+
+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)