Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,124 +8,120 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
|
| 8 |
|
| 9 |
# File where encrypted data is stored
|
| 10 |
VAULT_FILE = "passwords.json"
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
# --- Core Security Logic ---
|
| 15 |
|
| 16 |
def get_cipher(master_password):
|
| 17 |
-
"""Derives a strong AES-256 key from the master password."""
|
| 18 |
-
salt = b'hassan_naseer_secure_salt_2026'
|
| 19 |
kdf = PBKDF2HMAC(
|
| 20 |
algorithm=hashes.SHA256(),
|
| 21 |
length=32,
|
| 22 |
-
salt=
|
| 23 |
iterations=150000,
|
| 24 |
)
|
| 25 |
key = base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
|
| 26 |
return Fernet(key)
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
if not master_pwd:
|
| 35 |
-
return "β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
try:
|
| 38 |
cipher = get_cipher(master_pwd)
|
| 39 |
-
|
| 40 |
-
if os.path.exists(VAULT_FILE):
|
| 41 |
-
with open(VAULT_FILE, "r") as f:
|
| 42 |
-
vault = json.load(f)
|
| 43 |
-
else:
|
| 44 |
-
vault = {}
|
| 45 |
|
| 46 |
if action == "Save":
|
| 47 |
if not service or not secret:
|
| 48 |
-
return "β οΈ
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
with open(VAULT_FILE, "w") as f:
|
| 54 |
-
json.dump(vault, f)
|
| 55 |
-
|
| 56 |
-
FAILED_ATTEMPTS = 0
|
| 57 |
-
return f"β
Encrypted & Saved: {service}"
|
| 58 |
|
| 59 |
elif action == "Retrieve":
|
| 60 |
-
if not service:
|
| 61 |
-
return "β οΈ Enter a service name to decrypt."
|
| 62 |
if service not in vault:
|
| 63 |
-
return
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
FAILED_ATTEMPTS = 0
|
| 68 |
-
return f"π Password for {service}: {decrypted_data}"
|
| 69 |
-
|
| 70 |
except Exception:
|
| 71 |
-
|
| 72 |
-
remaining = MAX_ATTEMPTS - FAILED_ATTEMPTS
|
| 73 |
-
return f"π« Access Denied! {remaining} attempts remaining."
|
| 74 |
|
| 75 |
# --- UI Styling ---
|
| 76 |
-
|
| 77 |
custom_css = """
|
| 78 |
-
.gradio-container { max-width:
|
| 79 |
-
.
|
| 80 |
-
|
| 81 |
-
.gradio-container { padding: 10px; }
|
| 82 |
-
h1 { font-size: 1.5em !important; }
|
| 83 |
-
}
|
| 84 |
"""
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
svc = gr.Textbox(label="Service", placeholder="e.g., Google, Bank, GitHub")
|
| 106 |
-
pwd = gr.Textbox(label="Password to Protect", type="password")
|
| 107 |
-
save_btn = gr.Button("π Encrypt & Store", variant="primary")
|
| 108 |
-
|
| 109 |
-
with gr.Tab(label="π Retrieve Password"):
|
| 110 |
-
with gr.Column():
|
| 111 |
-
search_svc = gr.Textbox(label="Service Name", placeholder="Enter service to search")
|
| 112 |
-
get_btn = gr.Button("π Decrypt & Show", variant="secondary")
|
| 113 |
-
|
| 114 |
-
status_output = gr.Textbox(label="Operation Status", interactive=False)
|
| 115 |
-
|
| 116 |
-
# Event Handlers
|
| 117 |
-
save_btn.click(
|
| 118 |
-
fn=lambda m, s, p: process_vault("Save", m, s, p),
|
| 119 |
-
inputs=[master_key, svc, pwd],
|
| 120 |
-
outputs=status_output
|
| 121 |
-
)
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
-
# Fixed: Removed title from here
|
| 131 |
demo.launch(css=custom_css)
|
|
|
|
| 8 |
|
| 9 |
# File where encrypted data is stored
|
| 10 |
VAULT_FILE = "passwords.json"
|
| 11 |
+
# In a real app, the salt would be stored securely or unique per user
|
| 12 |
+
SALT = b'hassan_naseer_permanent_salt_2026'
|
| 13 |
|
| 14 |
# --- Core Security Logic ---
|
| 15 |
|
| 16 |
def get_cipher(master_password):
|
|
|
|
|
|
|
| 17 |
kdf = PBKDF2HMAC(
|
| 18 |
algorithm=hashes.SHA256(),
|
| 19 |
length=32,
|
| 20 |
+
salt=SALT,
|
| 21 |
iterations=150000,
|
| 22 |
)
|
| 23 |
key = base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
|
| 24 |
return Fernet(key)
|
| 25 |
|
| 26 |
+
def load_vault():
|
| 27 |
+
if os.path.exists(VAULT_FILE):
|
| 28 |
+
with open(VAULT_FILE, "r") as f:
|
| 29 |
+
return json.load(f)
|
| 30 |
+
return {}
|
| 31 |
+
|
| 32 |
+
def save_to_vault(vault_data):
|
| 33 |
+
with open(VAULT_FILE, "w") as f:
|
| 34 |
+
json.dump(vault_data, f)
|
| 35 |
+
|
| 36 |
+
# --- App Logic ---
|
| 37 |
|
| 38 |
+
def login(master_pwd):
|
| 39 |
if not master_pwd:
|
| 40 |
+
return gr.update(visible=True), gr.update(visible=False), "β οΈ Enter Password"
|
| 41 |
+
|
| 42 |
+
# We verify the password by attempting to derive the key
|
| 43 |
+
try:
|
| 44 |
+
# If the vault exists, we try to decrypt the first key as a test
|
| 45 |
+
vault = load_vault()
|
| 46 |
+
if vault:
|
| 47 |
+
cipher = get_cipher(master_pwd)
|
| 48 |
+
# Try decrypting the first stored item to verify password
|
| 49 |
+
test_key = list(vault.keys())[0]
|
| 50 |
+
cipher.decrypt(vault[test_key].encode())
|
| 51 |
+
|
| 52 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
| 53 |
+
except Exception:
|
| 54 |
+
# If decryption fails, password is wrong (unless vault is empty)
|
| 55 |
+
if not load_vault():
|
| 56 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
| 57 |
+
return gr.update(visible=True), gr.update(visible=False), "π« Incorrect Master Password"
|
| 58 |
|
| 59 |
+
def process_action(action, master_pwd, service=None, secret=None):
|
| 60 |
try:
|
| 61 |
cipher = get_cipher(master_pwd)
|
| 62 |
+
vault = load_vault()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if action == "Save":
|
| 65 |
if not service or not secret:
|
| 66 |
+
return "β οΈ Fill all fields"
|
| 67 |
+
vault[service] = cipher.encrypt(secret.encode()).decode()
|
| 68 |
+
save_to_vault(vault)
|
| 69 |
+
return f"β
Saved: {service}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
elif action == "Retrieve":
|
|
|
|
|
|
|
| 72 |
if service not in vault:
|
| 73 |
+
return "β Not found"
|
| 74 |
+
decrypted = cipher.decrypt(vault[service].encode()).decode()
|
| 75 |
+
return f"π Password for {service}: {decrypted}"
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception:
|
| 78 |
+
return "π« Critical Error: Check Master Password"
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# --- UI Styling ---
|
|
|
|
| 81 |
custom_css = """
|
| 82 |
+
.gradio-container { max-width: 600px; margin: auto; padding: 20px; }
|
| 83 |
+
.login-box { border: 1px solid #ddd; padding: 30px; border-radius: 15px; background: #fdfdfd; }
|
| 84 |
+
.vault-box { padding: 20px; }
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
| 86 |
|
| 87 |
+
with gr.Blocks(title="SecureVault Pro | Hassan Naseer") as demo:
|
| 88 |
+
|
| 89 |
+
# 1. LOGIN PANEL
|
| 90 |
+
with gr.Column(visible=True, elem_classes="login-box") as login_panel:
|
| 91 |
+
gr.Markdown("# π SecureVault Login")
|
| 92 |
+
gr.Markdown("Enter your Master Password to decrypt your history.")
|
| 93 |
+
master_input = gr.Textbox(label="Master Password", type="password")
|
| 94 |
+
login_btn = gr.Button("Unlock Vault", variant="primary")
|
| 95 |
+
login_msg = gr.Markdown(color="red")
|
| 96 |
+
|
| 97 |
+
# 2. MAIN VAULT PANEL (Hidden by default)
|
| 98 |
+
with gr.Column(visible=False, elem_classes="vault-box") as vault_panel:
|
| 99 |
+
gr.HTML(f"""<h2 style='text-align:center;'>Welcome Back, Hassan</h2><p style='text-align:center; color:grey;'>AES-256 Encrypted History Loaded</p>""")
|
| 100 |
+
|
| 101 |
+
with gr.Tabs():
|
| 102 |
+
with gr.Tab(label="β Add New"):
|
| 103 |
+
svc = gr.Textbox(label="Service")
|
| 104 |
+
pwd = gr.Textbox(label="Password", type="password")
|
| 105 |
+
save_btn = gr.Button("Save Encrypted", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
with gr.Tab(label="π History/Retrieve"):
|
| 108 |
+
search_svc = gr.Textbox(label="Search Service")
|
| 109 |
+
get_btn = gr.Button("Decrypt & Show", variant="secondary")
|
| 110 |
+
|
| 111 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 112 |
+
logout_btn = gr.Button("π Lock Vault", variant="stop", size="sm")
|
| 113 |
+
|
| 114 |
+
# --- Event Handlers ---
|
| 115 |
+
|
| 116 |
+
# Login Flow
|
| 117 |
+
login_btn.click(login, [master_input], [login_panel, vault_panel, login_msg])
|
| 118 |
+
|
| 119 |
+
# Action Flow
|
| 120 |
+
save_btn.click(lambda m, s, p: process_action("Save", m, s, p), [master_input, svc, pwd], status_output)
|
| 121 |
+
get_btn.click(lambda m, s: process_action("Retrieve", m, s), [master_input, search_svc], status_output)
|
| 122 |
+
|
| 123 |
+
# Logout Flow
|
| 124 |
+
logout_btn.click(lambda: [gr.update(visible=True), gr.update(visible=False), ""], None, [login_panel, vault_panel, master_input])
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 127 |
demo.launch(css=custom_css)
|