hassan773 commited on
Commit
92e0a91
Β·
verified Β·
1 Parent(s): 2b91292

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ import base64
5
+ from cryptography.fernet import Fernet
6
+ from cryptography.hazmat.primitives import hashes
7
+ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
8
+
9
+ # File where encrypted data is stored
10
+ VAULT_FILE = "passwords.json"
11
+
12
+ # --- Encryption Logic ---
13
+
14
+ def get_cipher(master_password):
15
+ """Derives a cryptographic key from the master password."""
16
+ # Use a fixed salt for this project; in production, this should be unique.
17
+ salt = b'nutech_hassan_salt_123'
18
+ kdf = PBKDF2HMAC(
19
+ algorithm=hashes.SHA256(),
20
+ length=32,
21
+ salt=salt,
22
+ iterations=100000,
23
+ )
24
+ key = base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
25
+ return Fernet(key)
26
+
27
+ def process_vault(action, master_pwd, service=None, secret=None):
28
+ if not master_pwd:
29
+ return "⚠️ Please enter your Master Password first!"
30
+
31
+ try:
32
+ cipher = get_cipher(master_pwd)
33
+
34
+ # Load the vault
35
+ if os.path.exists(VAULT_FILE):
36
+ with open(VAULT_FILE, "r") as f:
37
+ vault = json.load(f)
38
+ else:
39
+ vault = {}
40
+
41
+ if action == "Save":
42
+ if not service or not secret:
43
+ return "⚠️ Service name and Password are required!"
44
+ encrypted_data = cipher.encrypt(secret.encode()).decode()
45
+ vault[service] = encrypted_data
46
+ with open(VAULT_FILE, "w") as f:
47
+ json.dump(vault, f)
48
+ return f"βœ… Successfully encrypted and saved password for {service}."
49
+
50
+ elif action == "Retrieve":
51
+ if service not in vault:
52
+ return f"❌ No record found for '{service}'."
53
+ decrypted_data = cipher.decrypt(vault[service].encode()).decode()
54
+ return f"πŸ”‘ Password for {service}: {decrypted_data}"
55
+
56
+ except Exception:
57
+ return "🚫 Access Denied: Incorrect Master Password or corrupted data."
58
+
59
+ # --- UI Setup ---
60
+
61
+ custom_css = """
62
+ .gradio-container { max-width: 700px; margin: auto; }
63
+ .header-text { text-align: center; margin-bottom: 20px; }
64
+ .header-text h1 { margin: 0; }
65
+ .header-text span { font-size: 0.8em; color: gray; }
66
+ """
67
+
68
+ with gr.Blocks(css=custom_css) as demo:
69
+ gr.HTML("""
70
+ <div class="header-text">
71
+ <h1>πŸ” SecureVault Manager</h1>
72
+ <span>Developed by Hassan Naseer</span>
73
+ </div>
74
+ """)
75
+
76
+ with gr.Row():
77
+ master_key = gr.Textbox(
78
+ label="Master Password",
79
+ type="password",
80
+ placeholder="Enter your Master Password to unlock the vault",
81
+ info="This password is used to generate your unique encryption key."
82
+ )
83
+
84
+ with gr.Tabs():
85
+ with gr.TabItem("βž• Add New Password"):
86
+ with gr.Column():
87
+ svc = gr.Textbox(label="Service Name", placeholder="e.g., GitHub, Facebook")
88
+ pwd = gr.Textbox(label="Password", type="password", placeholder="Enter password to encrypt")
89
+ save_btn = gr.Button("Encrypt & Save", variant="primary")
90
+
91
+ with gr.TabItem("πŸ” Retrieve Password"):
92
+ with gr.Column():
93
+ search_svc = gr.Textbox(label="Service Name", placeholder="Enter the name of the service")
94
+ get_btn = gr.Button("Decrypt & Reveal", variant="secondary")
95
+
96
+ status_output = gr.Textbox(label="Vault Status", interactive=False)
97
+
98
+ # Event Handlers
99
+ save_btn.click(process_vault, ["Save", master_key, svc, pwd], status_output)
100
+ get_btn.click(process_vault, ["Retrieve", master_key, search_svc], status_output)
101
+
102
+ if __name__ == "__main__":
103
+ demo.launch()