hassan773 commited on
Commit
5c95943
Β·
verified Β·
1 Parent(s): 12f0a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -87
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
- FAILED_ATTEMPTS = 0
12
- MAX_ATTEMPTS = 5
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=salt,
23
  iterations=150000,
24
  )
25
  key = base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
26
  return Fernet(key)
27
 
28
- def process_vault(action, master_pwd, service=None, secret=None):
29
- global FAILED_ATTEMPTS
30
-
31
- if FAILED_ATTEMPTS >= MAX_ATTEMPTS:
32
- return "πŸ›‘ SECURITY LOCKOUT: Too many failed attempts. Access disabled."
 
 
 
 
 
 
33
 
 
34
  if not master_pwd:
35
- return "⚠️ Master Password required to access Vault."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 "⚠️ Service name and Password are required!"
49
-
50
- encrypted_data = cipher.encrypt(secret.encode()).decode()
51
- vault[service] = encrypted_data
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 f"❌ No record found for '{service}'."
64
-
65
- decrypted_data = cipher.decrypt(vault[service].encode()).decode()
66
 
67
- FAILED_ATTEMPTS = 0
68
- return f"πŸ”‘ Password for {service}: {decrypted_data}"
69
-
70
  except Exception:
71
- FAILED_ATTEMPTS += 1
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: 750px; margin: auto; padding: 20px; }
79
- .header-box { text-align: center; margin-bottom: 25px; border-bottom: 1px solid #ddd; padding-bottom: 10px; }
80
- @media (max-width: 768px) {
81
- .gradio-container { padding: 10px; }
82
- h1 { font-size: 1.5em !important; }
83
- }
84
  """
85
 
86
- # Fixed: Moved title to gr.Blocks constructor
87
- with gr.Blocks(title="SecureVault | Hassan Naseer") as demo:
88
- gr.HTML("""
89
- <div class="header-box">
90
- <h1 style="display:inline; color: #2d3436;">πŸ” SecureVault Manager</h1>
91
- <p style="font-size: 0.9em; color: #636e72;">Developed by Hassan Naseer | AES-256 Encryption</p>
92
- </div>
93
- """)
94
-
95
- with gr.Row():
96
- master_key = gr.Textbox(
97
- label="Unlock Vault with Master Password",
98
- type="password",
99
- placeholder="Enter Master Password..."
100
- )
101
-
102
- with gr.Tabs():
103
- with gr.Tab(label="πŸ“₯ Add Password"):
104
- with gr.Column():
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
- get_btn.click(
124
- fn=lambda m, s: process_vault("Retrieve", m, s),
125
- inputs=[master_key, search_svc],
126
- outputs=status_output
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)