Update keylock_component.py
Browse files- keylock_component.py +9 -12
keylock_component.py
CHANGED
@@ -15,7 +15,7 @@ import numpy as np
|
|
15 |
|
16 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
17 |
|
18 |
-
# --- Backend Logic Class
|
19 |
class AppServerLogic:
|
20 |
"""Contains all backend logic and mock data needed by the application."""
|
21 |
def __init__(self):
|
@@ -62,10 +62,7 @@ class AppServerLogic:
|
|
62 |
return {"status": "Error", "message": f"Decryption Failed: {e}"}
|
63 |
|
64 |
def generate_encrypted_image(self, payload_dict):
|
65 |
-
"""
|
66 |
-
Creates a KeyLock image and returns both a Pillow object for preview
|
67 |
-
and a filepath to a saved, uncorrupted PNG for download.
|
68 |
-
"""
|
69 |
try:
|
70 |
response = requests.get("https://images.unsplash.com/photo-1506318137071-a8e063b4bec0?q=80&w=1200", timeout=10)
|
71 |
img = ImageOps.fit(Image.open(io.BytesIO(response.content)).convert("RGB"), (600, 600))
|
@@ -84,11 +81,10 @@ class AppServerLogic:
|
|
84 |
pixel_data[:len(binary_payload)] = (pixel_data[:len(binary_payload)] & 0xFE) | np.array(list(binary_payload), dtype=np.uint8)
|
85 |
final_image = Image.fromarray(pixel_data.reshape(img.size[1], img.size[0], 3), 'RGB')
|
86 |
|
87 |
-
# Save the pristine PNG to a temporary file on the server.
|
88 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
89 |
final_image.save(f.name, "PNG")
|
90 |
-
# Return both the
|
91 |
-
return
|
92 |
|
93 |
@staticmethod
|
94 |
def generate_pem_keys():
|
@@ -97,7 +93,7 @@ class AppServerLogic:
|
|
97 |
pub = pk.public_key().public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo).decode()
|
98 |
return priv, pub
|
99 |
|
100 |
-
# --- UI Component Class
|
101 |
class KeylockDecoderComponent(gr.components.Component):
|
102 |
"""A Gradio Component that decodes a KeyLock image and provides advanced tools."""
|
103 |
EVENTS = ["change"]
|
@@ -132,15 +128,16 @@ class KeylockDecoderComponent(gr.components.Component):
|
|
132 |
gr.Markdown("Create a test image using the site's public key.")
|
133 |
payload_input = gr.JSON(label="Payload to Encrypt", value={"API_KEY": "sk-12345-abcde", "USER": "demo-user"})
|
134 |
generate_img_button = gr.Button("Generate Image", variant="secondary")
|
135 |
-
|
|
|
136 |
generated_file_download = gr.File(label="Download Uncorrupted PNG", interactive=False)
|
137 |
|
138 |
with gr.Accordion("Create New Standalone Key Pair", open=False):
|
139 |
gr.Markdown("Generate a new, random key pair for testing or other uses.")
|
140 |
generate_keys_button = gr.Button("Generate Keys", variant="secondary")
|
141 |
with gr.Row():
|
142 |
-
output_private_key = gr.Code(label="Generated Private Key", language="
|
143 |
-
output_public_key = gr.Code(label="Generated Public Key", language="
|
144 |
|
145 |
def on_image_upload(image):
|
146 |
if image is None: return None, "Upload a KeyLock image to auto-fill credentials."
|
|
|
15 |
|
16 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
17 |
|
18 |
+
# --- Backend Logic Class ---
|
19 |
class AppServerLogic:
|
20 |
"""Contains all backend logic and mock data needed by the application."""
|
21 |
def __init__(self):
|
|
|
62 |
return {"status": "Error", "message": f"Decryption Failed: {e}"}
|
63 |
|
64 |
def generate_encrypted_image(self, payload_dict):
|
65 |
+
"""Creates a KeyLock image and returns its filepath for both preview and download."""
|
|
|
|
|
|
|
66 |
try:
|
67 |
response = requests.get("https://images.unsplash.com/photo-1506318137071-a8e063b4bec0?q=80&w=1200", timeout=10)
|
68 |
img = ImageOps.fit(Image.open(io.BytesIO(response.content)).convert("RGB"), (600, 600))
|
|
|
81 |
pixel_data[:len(binary_payload)] = (pixel_data[:len(binary_payload)] & 0xFE) | np.array(list(binary_payload), dtype=np.uint8)
|
82 |
final_image = Image.fromarray(pixel_data.reshape(img.size[1], img.size[0], 3), 'RGB')
|
83 |
|
|
|
84 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
85 |
final_image.save(f.name, "PNG")
|
86 |
+
# Return the filepath for both the preview image and the download file.
|
87 |
+
return f.name, f.name
|
88 |
|
89 |
@staticmethod
|
90 |
def generate_pem_keys():
|
|
|
93 |
pub = pk.public_key().public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo).decode()
|
94 |
return priv, pub
|
95 |
|
96 |
+
# --- UI Component Class ---
|
97 |
class KeylockDecoderComponent(gr.components.Component):
|
98 |
"""A Gradio Component that decodes a KeyLock image and provides advanced tools."""
|
99 |
EVENTS = ["change"]
|
|
|
128 |
gr.Markdown("Create a test image using the site's public key.")
|
129 |
payload_input = gr.JSON(label="Payload to Encrypt", value={"API_KEY": "sk-12345-abcde", "USER": "demo-user"})
|
130 |
generate_img_button = gr.Button("Generate Image", variant="secondary")
|
131 |
+
# This component now expects a filepath to display the uncorrupted PNG.
|
132 |
+
generated_image_preview = gr.Image(label="Generated Image Preview", type="filepath", interactive=False)
|
133 |
generated_file_download = gr.File(label="Download Uncorrupted PNG", interactive=False)
|
134 |
|
135 |
with gr.Accordion("Create New Standalone Key Pair", open=False):
|
136 |
gr.Markdown("Generate a new, random key pair for testing or other uses.")
|
137 |
generate_keys_button = gr.Button("Generate Keys", variant="secondary")
|
138 |
with gr.Row():
|
139 |
+
output_private_key = gr.Code(label="Generated Private Key", language="pem")
|
140 |
+
output_public_key = gr.Code(label="Generated Public Key", language="pem")
|
141 |
|
142 |
def on_image_upload(image):
|
143 |
if image is None: return None, "Upload a KeyLock image to auto-fill credentials."
|