r/PythonLearning • • Oct 02 '25

Day 5 of 100 days of python

Post image

Made a password generator so hackers wont be able to hack yall accounts ;).Rate my ATM!

93 Upvotes

12 comments sorted by

7

u/Alagarto72 Oct 02 '25

I think you don't need to firstly define variable password as str and after the for loops make it into list. You can write password = [] and use password.append in for loops, because it's more efficient than string += other_string. In case with append you mutate the list, but with str you create new object every time.

4

u/AgentOfDreadful Oct 02 '25 edited Oct 02 '25

You could use random.SystemRandom() as its more cryptographically secure, as well as using the strings library:

all_chars = string.punctuation.replace("'", '') + string.ascii_letters + string.digits
    usable_chars = [char for char in all_chars if char not in arguments]
    password_string = ''.join(tuple(random.SystemRandom().choice(usable_chars) for _ in range(arguments.length)))

Full script including using argparse to avoid inputs:

#! /usr/bin/env python3

import argparse
import platform
import random
import string
import subprocess


def main():
    parser = argparse.ArgumentParser(description='Random password generator')
    parser.add_argument(
        'length',
        nargs='?',
        default=32, type=int,
        help='Password length'
    )
    parser.add_argument(
        '--no-clipboard',
        action='store_true', default=False,
        help='Use this if you do not want to automatically copy the password to your clipboard'
    )
    parser.add_argument(
        '--excluded', '-e',
        default=tuple(), type=tuple,
        required=False,
        help='Do not include specified characters. Example: "python3 password_generator.py --excluded "![],.!@#$"'
    )
    parser.add_argument(
        '--no-clip', '-n',
        action='store_true', default=False,
        help='Do not automatically copy the password to the clipboard (prints to STDOUT)'
    )
    arguments = parser.parse_args()

    all_chars = string.punctuation.replace("'", '') + string.ascii_letters + string.digits
    usable_chars = [char for char in all_chars if char not in arguments.excluded]
    password_string = ''.join(tuple(random.SystemRandom().choice(usable_chars) for _ in range(arguments.length)))

    clipboard_command = {
        'macos': 'pbcopy',
        'linux': 'xclip -select clipboard',
        'windo': 'clip'
    }.get(platform.platform().lower()[:5])

    if not arguments.no_clip:
        subprocess.check_call(f"echo '{password_string}' | {clipboard_command}", shell=True)
    else:
        print(password_string)


if __name__ == '__main__':
    main()

4

u/ryhartattack Oct 02 '25
usable_chars = [char for char in all_chars if char not in arguments.excluded]

1

u/AgentOfDreadful Oct 02 '25

Oops, good spot. Thank you

1

u/CombatWorthy_Wombat Oct 05 '25

If you wanted to use crypto random modules, secrets.rand is pretty good too 👍

1

u/mahousenshi Oct 02 '25

I don't know if you can use but have a look on the string module.

1

u/princepii Oct 02 '25

from string import ascii_printable as pt

imports a combination of lower upper case letters, digits and symbols or if u need them seperate just use:

from string import ( ascii_letters as al, digits as dt, punctuation as pt )

a = al+dt+pt+"your own few symbols or stuff"

if you wanna random that list and shuffle em u could use from random import shuffle or sample.

works better than write all of it yourself

1

u/Former_Spirit_5099 Oct 03 '25

what is this horror. Use `strings` module, for ASCII characters.

1

u/purple_hamster66 Oct 04 '25

What is the purpose of line 10?

1

u/sarc-tastic Oct 06 '25

Try random.choices!

-1

u/TheRNGuy Oct 02 '25

So it goes letters first, then numbers, then symbols? It should be randomly mixed.

Make some letters uppercase.

Some places only allow - and _ symbols (You didn't even added them)

5

u/Alagarto72 Oct 02 '25

No, there is random.shuffle at the 20 line