devhimal / password-generator-in-python

Simple password generator in python programming.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Step-by-Step Explanation

Imports and Constants

import random
import string
  • random: This module provides access to functions that support generating random numbers and selecting random elements.
  • string: This module contains a collection of string constants like digits, ascii_lowercase, and ascii_uppercase.

Define Constants

# Maximum length of password needed
MAX_LEN = 12

# Character sets
DIGITS = string.digits
LOCASE_CHARACTERS = string.ascii_lowercase
UPCASE_CHARACTERS = string.ascii_uppercase
SYMBOLS = '@#$%=:?./|~>*()<'
  • MAX_LEN: The desired length of the password (12 characters in this case).
  • DIGITS: Contains the string '0123456789', representing all digits.
  • LOCASE_CHARACTERS: Contains the string 'abcdefghijklmnopqrstuvwxyz', representing all lowercase letters.
  • UPCASE_CHARACTERS: Contains the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', representing all uppercase letters.
  • SYMBOLS: A string of special characters that can be used in the password.

Combine All Characters

# Combined list of all characters
COMBINED_LIST = DIGITS + LOCASE_CHARACTERS + UPCASE_CHARACTERS + SYMBOLS
  • COMBINED_LIST: A concatenated string of all possible characters that can be used in the password, combining digits, lowercase letters, uppercase letters, and special symbols.

Select One Character from Each Set

# Ensure the password has at least one character from each set
rand_digit = random.choice(DIGITS)
rand_upper = random.choice(UPCASE_CHARACTERS)
rand_lower = random.choice(LOCASE_CHARACTERS)
rand_symbol = random.choice(SYMBOLS)
  • random.choice(sequence): This function selects a random element from the given sequence.
  • rand_digit, rand_upper, rand_lower, rand_symbol: These variables store one randomly selected character from each of the character sets.

Combine and Extend the Password

# Combine the characters
temp_pass = rand_digit + rand_upper + rand_lower + rand_symbol

# Fill the rest of the password length with random choices from the combined list
for _ in range(MAX_LEN - 4):
    temp_pass += random.choice(COMBINED_LIST)
  • temp_pass: A temporary string that starts with the four selected characters, ensuring at least one character from each set is included.
  • for _ in range(MAX_LEN - 4): A loop that runs enough times to ensure the password reaches the desired length (MAX_LEN). In this case, it runs 8 times because we already have 4 characters in temp_pass.
  • temp_pass += random.choice(COMBINED_LIST): In each iteration, a random character from COMBINED_LIST is appended to temp_pass.

Shuffle and Finalize the Password

# Convert the temporary password into a list and shuffle to prevent patterns
temp_pass_list = array.array('u', temp_pass) or temp_pass_list = list(temp_pass)
random.shuffle(temp_pass_list)

# Form the final password
password = "Himal" #This code might be vary depending upon the users
for x in temp_pass_list: 
		password = password + x 
  • array.array('u', temp_pass) or list(temp_pass): Converts the string temp_pass into a list of characters to allow shuffling.
  • random.shuffle(temp_pass_list): Shuffles the list in place to ensure the characters are randomly ordered, preventing predictable patterns.

Print the Password

  • The loop concatenates each element of the temp_pass_list into a password variable.

Python

print() function prints the password that we generated.

Summary

The script generates a secure random password by ensuring it contains at least one digit, one uppercase letter, one lowercase letter, and one special symbol. It then fills the remaining length with random characters from a combined set, shuffles the characters to ensure randomness, and prints the final password. This ensures the password is strong and meets typical complexity requirements.

About

Simple password generator in python programming.


Languages

Language:Python 100.0%