hassan773 commited on
Commit
c7ed8c0
Β·
verified Β·
1 Parent(s): 282e341

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -67
app.py CHANGED
@@ -1,38 +1,66 @@
1
  import gradio as gr
2
  import re
 
3
 
4
- # 1. Helper function for requirement labels
5
- def get_label(is_met, text):
6
- color = "#28a745" if is_met else "#dc3545" # Green if met, Red if not
7
- symbol = "βœ…" if is_met else "❌"
8
- return f"<b style='color:{color}; font-size: 1.1em;'>{symbol} {text}</b>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # 2. Logic to build the checklist HTML and calculate strength
11
- def render_html(password=""):
12
  has_upper = re.search(r"[A-Z]", password)
13
  has_lower = re.search(r"[a-z]", password)
14
  has_num = re.search(r"\d", password)
15
  has_spec = re.search(r"[!@#$%^&*(),.?\":{}|<>]", password)
16
  has_len = len(password) >= 8
17
 
18
- # Adaptive background for Light/Dark mode compatibility
19
- checklist_html = f"""
20
- <div style="border: 1px solid #888; padding: 15px; border-radius: 10px; background-color: rgba(128, 128, 128, 0.05);">
21
- <p style="margin-top:0;"><b>Password Requirements:</b></p>
22
- {get_label(has_len, "8+ Characters")} &nbsp; | &nbsp;
23
- {get_label(has_upper, "Capital (A-Z)")} &nbsp; | &nbsp;
24
- {get_label(has_lower, "Small (a-z)")} <br><br>
25
- {get_label(has_num, "Number (0-9)")} &nbsp; | &nbsp;
26
- {get_label(has_spec, "Special (!@#)")}
27
  </div>
28
  """
29
-
30
  score = sum([bool(has_len), bool(has_upper), bool(has_lower), bool(has_num), bool(has_spec)])
31
- strength_pct = (score / 5) * 100
32
-
33
- return checklist_html, strength_pct
34
 
35
- # 3. Visibility and Dark Mode Logic
36
  def toggle_visibility(is_visible):
37
  new_type = "text" if is_visible else "password"
38
  new_label = "Hide Password πŸ‘οΈ" if is_visible else "Show Password πŸ‘οΈβ€πŸ—¨οΈ"
@@ -40,67 +68,36 @@ def toggle_visibility(is_visible):
40
 
41
  toggle_dark_js = """() => { document.body.classList.toggle('dark'); }"""
42
 
43
- # 4. Build the UI
44
  custom_css = """
45
  .gradio-container { max-width: 800px; margin: auto; }
46
  .avg-btn { max-width: 250px !important; margin: auto !important; width: 100%; }
47
- .button-row { justify-content: center !important; gap: 10px !important; }
48
  """
49
 
50
  with gr.Blocks() as demo:
51
- gr.Markdown("# πŸ›‘οΈ SmartPass Validator")
52
- gr.Markdown("Test your password and toggle settings below.")
53
-
54
  visible_state = gr.State(True)
55
 
56
  with gr.Row():
57
  with gr.Column(scale=3):
58
- pass_input = gr.Textbox(
59
- label="Your Password",
60
- placeholder="Start typing...",
61
- type="password",
62
- buttons=["copy"]
63
- )
64
- # Show/Hide button centered below input
65
- with gr.Row(elem_classes="button-row"):
66
- show_btn = gr.Button("Show Password πŸ‘οΈβ€πŸ—¨οΈ", variant="secondary", elem_classes="avg-btn")
67
-
68
  with gr.Column(scale=2):
69
- strength_meter = gr.Slider(
70
- label="Overall Strength Meter (%)",
71
- minimum=0, maximum=100, value=0, interactive=False
72
- )
73
 
74
- # Initialize with the requirements visible immediately
75
- initial_html, _ = render_html("")
76
- checklist_area = gr.HTML(value=initial_html)
77
 
78
- # Quick Settings Panel with equal width buttons
79
- gr.Markdown("### βš™οΈ Quick Settings")
80
- with gr.Row(elem_classes="button-row"):
81
- dark_btn = gr.Button("πŸŒ™ Dark Mode", variant="secondary", scale=1, elem_classes="avg-btn")
82
- reset_btn = gr.Button("πŸ—‘οΈ Clear", variant="stop", scale=1, elem_classes="avg-btn")
83
 
84
- # --- Event Triggers ---
85
- pass_input.change(
86
- fn=render_html,
87
- inputs=pass_input,
88
- outputs=[checklist_area, strength_meter]
89
- )
90
-
91
- show_btn.click(
92
- toggle_visibility,
93
- inputs=[visible_state],
94
- outputs=[pass_input, show_btn, visible_state]
95
- )
96
-
97
  dark_btn.click(None, None, None, js=toggle_dark_js)
98
-
99
- reset_btn.click(
100
- lambda: ["", initial_html, 0],
101
- None,
102
- [pass_input, checklist_area, strength_meter]
103
- )
104
 
105
  if __name__ == "__main__":
106
  demo.launch(theme=gr.themes.Soft(), css=custom_css)
 
1
  import gradio as gr
2
  import re
3
+ import math
4
 
5
+ # 1. Crack Time Logic
6
+ def estimate_crack_time(password):
7
+ if not password:
8
+ return "0 seconds"
9
+
10
+ # Calculate character set size (L)
11
+ L = 0
12
+ if re.search(r"[a-z]", password): L += 26
13
+ if re.search(r"[A-Z]", password): L += 26
14
+ if re.search(r"\d", password): L += 10
15
+ if re.search(r"[!@#$%^&*(),.?\":{}|<>]", password): L += 32
16
+
17
+ if L == 0: L = 1 # Avoid log errors
18
+
19
+ # Combinations = L ^ length
20
+ combinations = math.pow(L, len(password))
21
+
22
+ # Assuming 10 billion guesses per second (standard high-end brute force)
23
+ seconds = combinations / 10_000_000_000
24
+
25
+ if seconds < 1: return "Instantly ⚑"
26
+ if seconds < 60: return f"{int(seconds)} seconds"
27
+ if seconds < 3600: return f"{int(seconds/60)} minutes"
28
+ if seconds < 86400: return f"{int(seconds/3600)} hours"
29
+ if seconds < 31536000: return f"{int(seconds/86400)} days"
30
+ if seconds < 31536000000: return f"{int(seconds/31536000)} years"
31
+ return "Centuries 🏰"
32
+
33
+ # 2. Main Analysis Function
34
+ def analyze_password(password):
35
+ if not password:
36
+ return render_checklist(""), 0, "0 seconds"
37
+
38
+ checklist_html, strength = render_checklist(password)
39
+ crack_time = estimate_crack_time(password)
40
+
41
+ return checklist_html, strength, crack_time
42
 
43
+ def render_checklist(password):
 
44
  has_upper = re.search(r"[A-Z]", password)
45
  has_lower = re.search(r"[a-z]", password)
46
  has_num = re.search(r"\d", password)
47
  has_spec = re.search(r"[!@#$%^&*(),.?\":{}|<>]", password)
48
  has_len = len(password) >= 8
49
 
50
+ def get_label(is_met, text):
51
+ color = "#28a745" if is_met else "#dc3545"
52
+ symbol = "βœ…" if is_met else "❌"
53
+ return f"<b style='color:{color};'>{symbol} {text}</b>"
54
+
55
+ html = f"""
56
+ <div style="border: 1px solid #888; padding: 15px; border-radius: 10px; background-color: rgba(128,128,128,0.05);">
57
+ <p><b>Requirements:</b></p>
58
+ {get_label(has_len, "8+ Chars")} | {get_label(has_upper, "Upper")} | {get_label(has_lower, "Lower")} | {get_label(has_num, "Number")} | {get_label(has_spec, "Special")}
59
  </div>
60
  """
 
61
  score = sum([bool(has_len), bool(has_upper), bool(has_lower), bool(has_num), bool(has_spec)])
62
+ return html, (score / 5) * 100
 
 
63
 
 
64
  def toggle_visibility(is_visible):
65
  new_type = "text" if is_visible else "password"
66
  new_label = "Hide Password πŸ‘οΈ" if is_visible else "Show Password πŸ‘οΈβ€πŸ—¨οΈ"
 
68
 
69
  toggle_dark_js = """() => { document.body.classList.toggle('dark'); }"""
70
 
71
+ # 3. UI Build
72
  custom_css = """
73
  .gradio-container { max-width: 800px; margin: auto; }
74
  .avg-btn { max-width: 250px !important; margin: auto !important; width: 100%; }
75
+ .time-display { font-size: 1.5em; font-weight: bold; color: #ff4b4b; }
76
  """
77
 
78
  with gr.Blocks() as demo:
79
+ gr.Markdown("# πŸ›‘οΈ SecurePass Pro Analyzer")
 
 
80
  visible_state = gr.State(True)
81
 
82
  with gr.Row():
83
  with gr.Column(scale=3):
84
+ pass_input = gr.Textbox(label="Enter Password", type="password", buttons=["copy"])
85
+ show_btn = gr.Button("Show Password πŸ‘οΈβ€πŸ—¨οΈ", variant="secondary", elem_classes="avg-btn")
 
 
 
 
 
 
 
 
86
  with gr.Column(scale=2):
87
+ strength_meter = gr.Slider(label="Strength (%)", interactive=False)
88
+ gr.Markdown("### πŸ•’ Estimated Crack Time:")
89
+ time_output = gr.Markdown("0 seconds", elem_classes="time-display")
 
90
 
91
+ checklist_area = gr.HTML(value=render_checklist("")[0])
 
 
92
 
93
+ with gr.Row():
94
+ dark_btn = gr.Button("πŸŒ™ Dark Mode", variant="secondary", elem_classes="avg-btn")
95
+ reset_btn = gr.Button("πŸ—‘οΈ Clear", variant="stop", elem_classes="avg-btn")
 
 
96
 
97
+ pass_input.change(analyze_password, pass_input, [checklist_area, strength_meter, time_output])
98
+ show_btn.click(toggle_visibility, [visible_state], [pass_input, show_btn, visible_state])
 
 
 
 
 
 
 
 
 
 
 
99
  dark_btn.click(None, None, None, js=toggle_dark_js)
100
+ reset_btn.click(lambda: ["", render_checklist("")[0], 0, "0 seconds"], None, [pass_input, checklist_area, strength_meter, time_output])
 
 
 
 
 
101
 
102
  if __name__ == "__main__":
103
  demo.launch(theme=gr.themes.Soft(), css=custom_css)