before custom
This commit is contained in:
@@ -58,6 +58,5 @@ if __name__ == "__main__":
|
||||
decryptor = ConfigDecryptor(DECRYPTION_KEY)
|
||||
try:
|
||||
config = decryptor.decrypt()
|
||||
print(config)
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
|
||||
@@ -1,53 +1,77 @@
|
||||
"""
|
||||
Module for encrypting configuration files using Fernet symmetric encryption.
|
||||
"""
|
||||
|
||||
from cryptography.fernet import Fernet
|
||||
import json
|
||||
|
||||
|
||||
class ConfigEncryptor:
|
||||
"""
|
||||
Class to handle encryption of configuration data.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize the ConfigEncryptor with a generated encryption key.
|
||||
"""
|
||||
self.key = Fernet.generate_key()
|
||||
def __init__(self, key, filename="config.enc"):
|
||||
self.key = key
|
||||
self.filename = filename
|
||||
self.fernet = Fernet(self.key)
|
||||
|
||||
def encrypt_config(self, data):
|
||||
"""
|
||||
Encrypt the configuration data and save it to 'config.enc'.
|
||||
|
||||
Args:
|
||||
data (str): The configuration data to be encrypted.
|
||||
"""
|
||||
fernet = Fernet(self.key)
|
||||
encrypted = fernet.encrypt(data.encode())
|
||||
with open("config.enc", "wb") as encrypted_file:
|
||||
encrypted_file.write(encrypted)
|
||||
json_data = json.dumps(data)
|
||||
encrypted_data = self.fernet.encrypt(json_data.encode())
|
||||
with open(self.filename, "wb") as encrypted_file:
|
||||
encrypted_file.write(encrypted_data)
|
||||
|
||||
def get_key(self):
|
||||
"""
|
||||
Get the generated encryption key.
|
||||
|
||||
Returns:
|
||||
str: The generated encryption key as a string.
|
||||
"""
|
||||
return self.key.decode()
|
||||
|
||||
def save_credentials(self, credentials):
|
||||
config = self.load_config()
|
||||
if not config:
|
||||
config = {"credentials": {}, "options": {}}
|
||||
config["credentials"] = credentials
|
||||
self.encrypt_config(config)
|
||||
|
||||
def save_options(self, options):
|
||||
config = self.load_config()
|
||||
if not config:
|
||||
config = {"credentials": {}, "options": {}}
|
||||
# Ensure options only contains serializable data
|
||||
serializable_options = {k: v for k, v in options.items() if self.is_json_serializable(v)}
|
||||
config["options"] = serializable_options
|
||||
self.encrypt_config(config)
|
||||
|
||||
def load_config(self):
|
||||
try:
|
||||
with open(self.filename, "rb") as encrypted_file:
|
||||
encrypted_data = encrypted_file.read()
|
||||
decrypted_data = self.fernet.decrypt(encrypted_data).decode()
|
||||
return json.loads(decrypted_data)
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def is_json_serializable(value):
|
||||
try:
|
||||
json.dumps(value)
|
||||
return True
|
||||
except (TypeError, OverflowError):
|
||||
return False
|
||||
|
||||
|
||||
# Define your key here
|
||||
# Replace with your actual key
|
||||
key = b"u4xTBY5Ns4WYdLvqMjEr138mpMmDEhhqTszKCcDy2cI="
|
||||
|
||||
if __name__ == "__main__":
|
||||
CONFIG_DATA = """
|
||||
{
|
||||
"url": "https://yourstore.com",
|
||||
"consumer_key": "ck_yourconsumerkey",
|
||||
"consumer_secret": "cs_yoursecret",
|
||||
"username": "yourusername",
|
||||
"password": "yourpassword"
|
||||
config_data = {
|
||||
"credentials": {
|
||||
"url": "https://yourstore.com",
|
||||
"consumer_key": "ck_yourconsumerkey",
|
||||
"consumer_secret": "cs_yoursecret",
|
||||
"username": "yourusername",
|
||||
"password": "yourpassword"
|
||||
},
|
||||
"options": {
|
||||
"canvas_width": 900,
|
||||
"canvas_height": 900,
|
||||
"template": "{slug}_{sku}_{width}x{height}",
|
||||
"delete_images": False,
|
||||
"background_color": "#FFFFFF"
|
||||
}
|
||||
}
|
||||
"""
|
||||
encryptor = ConfigEncryptor()
|
||||
print(f"Encryption key: {encryptor.get_key()}")
|
||||
encryptor.encrypt_config(CONFIG_DATA)
|
||||
encryptor = ConfigEncryptor(key)
|
||||
encryptor.encrypt_config(config_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user