Get code to generate a password
Instead of generating a password directly, this tool gives you code to generate a password. This means that you can see exactly how the password is generated, and don't need to trust this tool not to steal your password.
If you don't mind long passwords and don't care what special characters are included, you can run openssl rand -base64 16 to get a secure password. This is the easiest to verify as trustworthy, so this is a very good choice.
import secrets
letters_lower = 'abcdefghijklmnopqrstuvwxyz'
letters_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = '0123456789'
special = '!@#$%^&*()_+-=[]{}\\|;:\'\",<.>/?`~ '
all_characters = letters_lower + letters_upper + numbers + special
# A length of 20 gives you a a password with more than 128 bits of entropy
# This is more than enough entropy for every password key use case
length = 20
def random_char(characters):
return secrets.choice(characters)
def generate_password():
password = ""
for _ in range(length):
password += random_char(all_characters)
return password
def verify_password(password):
has_lower = any(char in letters_lower for char in password)
has_upper = any(char in letters_upper for char in password)
has_number = any(char in numbers for char in password)
has_special = any(char in special for char in password)
return has_lower and has_upper and has_number and has_special
password = generate_password()
while not verify_password(password):
password = generate_password()
print(password)