How to create a strong random password generator in python?



To be more secure online, the system random password generator is considered a very strong password since it generates a combination of letters, numbers, and special characters.

The program is dynamic in which the user will be asked to enter the length of the password they want to generate. Accordingly, it will generate a combination of scii_letters, digits, and punctuation.

There are two programs that use two modules; random and string modules. A random module is used to pick random letters, digits, and punctuation from the string module.

Code 1:

Random password generator sample code 1 will ask you to enter the length of the password you want to generate and the rest will be taken care of by the program. It will automatically pick random digits, letters, and punctuations and its combination will be generated for you. For more dynamic and complex programs check sample code 2.

import random
import string

password_length = int(input("Enter the length of password:"))

def generatingPassword():
    randomPassword = string.ascii_letters + string.digits + string.punctuation

    password = random.sample(randomPassword, password_length)
    
    password += random.choice(string.ascii_uppercase)
    password += random.choice(string.digits)
    password += random.choice(string.punctuation)

    passwordList = list(password)
    random.shuffle(passwordList)
    password = "".join(passwordList)
    
    return password

print(f"Password of length {password_length}: {generatingPassword()}")

Output:

>>>
Enter the length of password:7
Password of length 7: 8Z[WGw*"*J

>>>
Enter the length of password:18
Password of length 18: &NeAkrGS@Ha.T#B865^Z*


Code 2:

Random password generator sample code 2 will ask you to enter the length of the password more than 6. If you enter the length less than 6, it will once again ask you to enter the length. More importantly, the user gets control of how many letters, digits, and symbols you want in your password. Accordingly, you will get a strong random password generated for you that will keep you more secure online.

import random
import string

def randPassword(length,letters,num):
    password = ''
    letter = ''
    numb = ''
    for x in range(letters):
        letter += random.choice(string.ascii_letters)
    for y in range(num):
        numb += str(random.choice(string.digits + string.punctuation))
    for z in range(len(letter + numb)):
        password += random.choice(letter + numb)
    return password

tot = 0
n_digit = 0
while tot < 6:
    length = int(input("Enter length of password more then 6: "))
    tot = length
    if length < 6:
        print("Error:Your password length should be atleast 6!")
else:
    n_letter = length
    while n_letter >= length:
        letter = int(input("Total number of alphabet: "))
        n_letter = letter
        if letter >= length:
            print("Number of alphabet shoul be less than the length of password!")
    else:
        while n_digit != (length - letter):
            num = int(input("Number of numirical value and symbol: "))
            n_digit = num
            if num != (length - letter):
                print("Total number of numerical and symbol should be",length - letter,"!")
print("\nYour Password: ",randPassword(length,letter,num))

Output:

>>>
Enter length of password more then 6: 5
Error:Your password length should be atleast 6!
Enter length of password more then 6: 15
Total number of alphabet: 15
Number of alphabet shoul be less than the length of password!
Total number of alphabet: 10
Number of numirical value and symbol: 6
Total number of numerical and symbol should be 5 !
Number of numirical value and symbol: 5

Your Password:  %M9%9%UGXc8cb9%

>>>
Enter length of password more then 6: 10
Total number of alphabet: 5
Number of numirical value and symbol: 5

Your Password:  vv]VU?e"v$


Bonus:

The secrets module is most recommended to generate a secure random number for managing secrets as per python official documentation.

It was mentioned that the secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. In particular, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modeling and simulation, not security or cryptography.