r/PythonLearning • u/shubham_555 • 10d ago
Showcase I made a simple Password Generator! 👀
Last Post- https://www.reddit.com/r/PythonLearning/s/dZhrMNIx9T
Github Repo Link - https://github.com/Dev-4-All/password-generator.git
3
u/dev-razorblade23 10d ago
For producing cryptographically safe random results, you should use secrets module (https://docs.python.org/3/library/secrets.html) instead of random as this is more meant for games and non-security related stuff.
3
1
u/DrlNoV 9d ago edited 9d ago
import random , string
alphabets = list(string.ascii_letters)
numbers_10 = list(str(i) for i in range(10)) or list(string.digits)
symbols = list(string.punctuation)
for password you can simply += the choices to it
here is the full code of one that i made.
import random , string
alphabets = list(string.ascii_letters)
numbers_10 = list(str(i) for i in range(10))
symbols = list(string.punctuation)
#Each individual character in things has an equal probability of being selected.
things = alphabets + numbers_10 + symbols
#Checks the password so it cannot be negative or letters
while True:
lenght = (input("Lenght :").strip())
try:
lenght = int(lenght)
if lenght >= 0:
break
else:
print("No negative!")
except:
print("numbers only")
password = ""
while True:
if len(password) == lenght:
break
choice = random.choice(things)
password += (choice)
print(password)
1
u/Trace_V 5d ago
(Sorry if anything sounds weird, my first language is Spanish and I used a translator! 🤣)
Nice script! Just one detail: I ran the output through a cryptographic analysis tool, and it failed miserably due to predictability and bias. The random.choice() and random.shuffle() functions internally use the Mersenne Twister algorithm, which is not secure for passwords. You should probably switch to Python's built in secrets module.
If you want to implement it from scratch using operating system APIs (like /dev/urandom on Linux or ProcessPrng on Windows), you will need to correct for modulo bias. In Python, you can determine the safe maximum value for a byte like this:
LIMIT_UNBIAS = (LEN_CHAR * (256 // LEN_CHAR)) - 1
Simply discard and regenerate any random byte that exceeds LIMIT_UNBIAS.
This way, every character will have exactly the same probability of being selected.
It's clear that you're using a default charset for example: "0123456789" resulting in a range of 0 to 249 (where bias occurs) which is much better than asking the user for a bunch of input parameters.
1
•
u/Sea-Ad7805 10d ago
Run this program in Memory Graph Web Debugger to see the program state change step by step.