I wanted to research and develop something myself, and I was interested in password generation for a long time, so I decided to make it.
Since Python is a short story, I would like to make it easily using the Python library. Please note that the code is dirty because I am an amateur when it comes to development.
――I want to make it a class first, and finally a web application. --You can select the number of characters --You can select the type of characters to include --You can select something like "capital alphabet appears at least once".
As a major premise of passwords, ** something that cannot be guessed by others ** is recommended, so how to select characters is important. Therefore, we need a mechanism to randomly select characters.
Python has a random module. https://docs.python.org/ja/3/library/random.html#module-random
However, as stated in the official document above, it is only a random value to some extent that can be logically reproduced / guessed, and is not suitable for security purposes.
Therefore, it is recommended to use the secrets module based on the random number source provided by the OS for security purposes. https://docs.python.org/ja/3/library/secrets.html#module-secrets This is it. This is fine.
Moreover, if you look closely, you will find recipes and best practices.
To generate a 10-character password consisting of alphabeds and numbers, including at least one lowercase letter and at least three numbers:
generate_password_best_practice.py
import string
import secrets
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
Let's play with this.
generate_random.py
import string
import secrets
alphabet = string.ascii_letters + string.digits
def password_generater(password_length):
while True:
password = ''.join(secrets.choice(alphabet) for i in range(password_length))
if (any(c.islower() for c in password) and any(c.isupper() for c in password) and sum(c.isdigit() for c in password) >= 3):
break
return password
main.py
import generate_random
#Password length can be specified by argument
result = generate_random.password_generater(16)
print(result)
When I run it
7g29HnFJItLJoiB7
Yeah, it feels good.
generate_random.py
import string
import secrets
alphabet = string.ascii_letters + string.digits
def password_generater(password_length, lower_letters_min=0, upper_letters_min=0, digit_min=0):
while True:
password = ''.join(secrets.choice(alphabet) for i in range(password_length))
if (sum(c.islower() for c in password) >= lower_letters_min and sum(c.isupper() for c in password) >= upper_letters_min and sum(c.isdigit() for c in password) >= digit_min):
break
return password
Since it is set to 0 by default, it is generated once unless otherwise specified.
When I run it
zmoIBPQVU8MI3tJL
It seems that the condition that "at least three numbers appear" in the official recipe has certainly disappeared.
After trying so far, I came up with the idea that it would be more convenient to summarize the number of occurrences in a list when something like "I want to specify only the minimum number of uppercase letters!"
(Currently under construction)