Mohammed98 commited on
Commit
22b6e1b
Β·
verified Β·
1 Parent(s): 9c1b601

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -22
app.py CHANGED
@@ -1,33 +1,21 @@
1
  import gradio as gr
2
  import bcrypt
 
3
 
4
- def hash_password(password: str) -> str:
5
- if not password:
6
- return "❌ Please enter a password"
7
- pw_bytes = password.encode("utf-8")
8
  hashed = bcrypt.hashpw(pw_bytes, bcrypt.gensalt(rounds=12))
9
- return hashed.decode("utf-8")
10
-
11
- def toggle_password(show_password: bool):
12
- return gr.update(type="text" if show_password else "password")
13
 
14
  with gr.Blocks() as demo:
15
- gr.Markdown("## πŸ” Password Hasher (bcrypt)\nEnter a password to generate its bcrypt hash")
16
-
17
- with gr.Row():
18
- password_input = gr.Textbox(
19
- type="password", label="Enter your password", placeholder="Type password here..."
20
- )
21
- show_checkbox = gr.Checkbox(label="Show password", value=False)
22
-
23
- hash_output = gr.Textbox(label="Generated hash")
24
 
25
- # Button to generate hash
26
- hash_btn = gr.Button("Generate Hash")
27
- hash_btn.click(fn=hash_password, inputs=password_input, outputs=hash_output)
28
 
29
- # Toggle password visibility
30
- show_checkbox.change(fn=toggle_password, inputs=show_checkbox, outputs=password_input)
31
 
32
  if __name__ == "__main__":
33
  demo.launch()
 
1
  import gradio as gr
2
  import bcrypt
3
+ import secrets
4
 
5
+ def generate_hash() -> str:
6
+ # generate a random password/token each time
7
+ random_password = secrets.token_hex(8) # 16-char random string
8
+ pw_bytes = random_password.encode("utf-8")
9
  hashed = bcrypt.hashpw(pw_bytes, bcrypt.gensalt(rounds=12))
10
+ return f"πŸ”‘ Random password: {random_password}\n\nπŸ” Hash: {hashed.decode('utf-8')}"
 
 
 
11
 
12
  with gr.Blocks() as demo:
13
+ gr.Markdown("## πŸ” Random Password Hasher (bcrypt)\nClick the button to generate a random password and its hash")
 
 
 
 
 
 
 
 
14
 
15
+ output_box = gr.Textbox(label="Result", lines=4)
 
 
16
 
17
+ gen_btn = gr.Button("Generate Hash")
18
+ gen_btn.click(fn=generate_hash, inputs=None, outputs=output_box)
19
 
20
  if __name__ == "__main__":
21
  demo.launch()