diff options
author | Christian Cleberg <hello@cleberg.net> | 2024-11-06 21:52:32 -0600 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2024-11-06 21:52:32 -0600 |
commit | c7e5d9fbec9418d856be961358e35f603bbbf523 (patch) | |
tree | 1aa2bbdd0bf96d7037537c2d6f5a989c19be8ffd /src/crypto.py | |
parent | e5d61719e201a1c25e9907bd220bfcac4fc9a4f5 (diff) | |
download | yoshi-cli-c7e5d9fbec9418d856be961358e35f603bbbf523.tar.gz yoshi-cli-c7e5d9fbec9418d856be961358e35f603bbbf523.tar.bz2 yoshi-cli-c7e5d9fbec9418d856be961358e35f603bbbf523.zip |
configure as pypi hosted app
Diffstat (limited to 'src/crypto.py')
-rw-r--r-- | src/crypto.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/crypto.py b/src/crypto.py new file mode 100644 index 0000000..9b0a423 --- /dev/null +++ b/src/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) |