r/madeinpython • • Sep 27 '21

Simple Password Generator in under 10 lines

https://www.youtube.com/watch?v=PNlbGpbyKBA
30 Upvotes

7 comments sorted by

10

u/muikrad Sep 27 '21

This is kinda overkill, you can do it in one line :

print("hunter2")

6

u/jampk24 Sep 27 '21

Every time someone posts a password generator, they always use the random module. If you look at the documentation, it says in a nice red box not to use this module for security purposes. Use the secretes module instead.

4

u/Aixyn0 Sep 27 '21

from random import randrange

print('Password: '+''.join(chr(randrange(33, 127)) for i in range(8)))

2

u/SweetOnionTea Sep 27 '21

Lol apparently they don't teach ASCII anymore.

2

u/Aixyn0 Sep 27 '21

To be fair, my solution will most likely break all password validators because it uses the whole set of "special characters" (excluding space) which propably isn't allowed.

1

u/zynix Sep 27 '21 edited Sep 27 '21

Not for passwords but for verification tokens I do something like:

import string
import random

token = "".join([random.choice(string.printable) for i in range(16)])
print(f"{token=}")

edit: https://docs.python.org/3/library/string.html#string.printable
https://docs.python.org/3/library/random.html?highlight=random%20choice#random.choice

I've seen people use random.sample BUT the problem is that sample only uses 1 character in an iterable/sequence while random.choice could conceivably have 1+n repeats for more randomness.

https://docs.python.org/3/library/random.html?highlight=random%20choice#random.sample

1

u/[deleted] Oct 04 '21

It's fine to experiment with crypto, which passwords fall into since the are essentially keys that are meant not to be easily guessable, but don't use it for anything and don't publish it.

Security professionals don't write their crypto anything, they use frameworks developed by others who specialize in the practice.

They also avoid the random module, it was designed for modeling not security. Random was designed to produce the same "random" results consistently, which pretty much defines cryptographically weak. Look instead at [secrets](https://docs.python.org/3/library/secrets.html) instead. I use it over random even for purposes which have no strict security requirements,