File size: 3,540 Bytes
8396d4c
 
 
1fa369f
8396d4c
 
 
1fa369f
3ff2dea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa369f
8396d4c
3ff2dea
 
 
 
8396d4c
3ff2dea
 
8396d4c
3ff2dea
8396d4c
3ff2dea
 
8396d4c
3ff2dea
 
1fa369f
3ff2dea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8396d4c
3ff2dea
 
 
 
 
1fa369f
3ff2dea
1fa369f
 
3ff2dea
 
 
 
1fa369f
 
3ff2dea
 
 
 
 
8396d4c
 
3ff2dea
 
 
 
 
1fa369f
3ff2dea
 
 
 
1fa369f
3ff2dea
 
 
 
1fa369f
3ff2dea
1fa369f
3ff2dea
1fa369f
8396d4c
3ff2dea
 
 
 
 
8396d4c
 
3ff2dea
 
 
8396d4c
 
3ff2dea
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa369f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import gradio as gr
import string
import secrets
import math

SYMBOLS = "!@#$%^&*()-_=+[]{}|;:,.<>?/"

def analyze_password(length, pool_size):
    entropy = length * math.log2(pool_size)

    if entropy < 40:
        strength = "Weak"
        crack_time = "Seconds to Minutes"
    elif entropy < 60:
        strength = "Medium"
        crack_time = "Hours to Days"
    elif entropy < 80:
        strength = "Strong"
        crack_time = "Years"
    elif entropy < 100:
        strength = "Very Strong"
        crack_time = "Millions of Years"
    else:
        strength = "Extremely Strong"
        crack_time = "Practically Uncrackable"

    return entropy, strength, crack_time


def generate_password(length, letters, numbers, symbols, allow_repeats):
    try:
        length = int(length)
    except:
        return "Error", "Length must be a valid number."

    if length <= 0:
        return "Error", "Length must be greater than 0."

    pool = ""

    if letters:
        pool += string.ascii_letters

    if numbers:
        pool += string.digits

    if symbols:
        pool += SYMBOLS

    if not pool:
        return "Error", "Select at least one character type."

    unique_chars = len(set(pool))

    if not allow_repeats and length > unique_chars:
        selected = []

        if letters:
            selected.append("Letters")

        if numbers:
            selected.append("Numbers")

        if symbols:
            selected.append("Symbols")

        return (
            "Impossible Request",
            f"You selected: {', '.join(selected)}\n"
            f"Repeated characters disabled.\n\n"
            f"Only {unique_chars} unique characters are available,\n"
            f"but you requested {length} characters."
        )

    if allow_repeats:
        password = "".join(
            secrets.choice(pool)
            for _ in range(length)
        )
    else:
        password = "".join(
            secrets.SystemRandom().sample(pool, length)
        )

    entropy, strength, crack_time = analyze_password(
        len(password),
        len(set(pool))
    )

    analysis = (
        f"Strength: {strength}\n"
        f"Entropy: {entropy:.1f} bits\n"
        f"Estimated Crack Time: {crack_time}"
    )

    return password, analysis


with gr.Blocks(title="Password Generator V2") as demo:
    gr.Markdown("# 🔐 Password Generator V2")
    gr.Markdown(
        "Generate secure passwords and estimate their strength."
    )

    length = gr.Number(
        value=16,
        label="Password Length",
        minimum=1,
        precision=0
    )

    with gr.Row():
        letters = gr.Checkbox(
            value=True,
            label="Letters (A-Z, a-z)"
        )

        numbers = gr.Checkbox(
            value=True,
            label="Numbers (0-9)"
        )

        symbols = gr.Checkbox(
            value=False,
            label="Symbols"
        )

    allow_repeats = gr.Checkbox(
        value=True,
        label="Allow Repeated Characters"
    )

    generate_btn = gr.Button("Generate Password")

    password_output = gr.Textbox(
        label="Generated Password",
        lines=2
    )

    analysis_output = gr.Textbox(
        label="Password Analysis",
        lines=4
    )

    generate_btn.click(
        fn=generate_password,
        inputs=[
            length,
            letters,
            numbers,
            symbols,
            allow_repeats
        ],
        outputs=[
            password_output,
            analysis_output
        ]
    )

demo.launch()