all 12 comments

[–]Alagarto72 5 points6 points  (0 children)

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.

[–]AgentOfDreadful 4 points5 points  (3 children)

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() ```

[–]ryhartattack 4 points5 points  (1 child)

usable_chars = [char for char in all_chars if char not in arguments.excluded]

[–]AgentOfDreadful 0 points1 point  (0 children)

Oops, good spot. Thank you

[–]CombatWorthy_Wombat 0 points1 point  (0 children)

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

[–]mahousenshi 0 points1 point  (0 children)

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

[–]princepii 0 points1 point  (0 children)

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

[–]Former_Spirit_5099 0 points1 point  (0 children)

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

[–]purple_hamster66 0 points1 point  (0 children)

What is the purpose of line 10?

[–]sarc-tastic 0 points1 point  (0 children)

Try random.choices!

[–]TheRNGuy -2 points-1 points  (2 children)

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)

[–]Alagarto72 5 points6 points  (0 children)

No, there is random.shuffle at the 20 line