Generating a strong password using Python



Having a weak password is not good for a system that requires high confidentiality and security of user credentials. It turns out that people find it difficult to create a strong password strong enough to prevent unauthorized users from remembering it. 
This article uses a combination of numbers, alphabets, and other symbols found on a computer keyboard to form a 12-digit password that is unpredictable and cannot be easily remembered.

  • The components of a password are presented in as arrays.
  • Use the random method to select at least one character from each character array.
  • Since a 12-character password is required, so fill in the remainder of the password length with randomly selected characters from the array formed from a combination of all characters required in the final password. 
    whenever a password is generated, shuffle it with random.shuffle () to make sure the final password doesn`t match a certain pattern.

import random

import array

 
Maximum password length
# this can be changed according to your password length

MAX_LEN = 12

 
# declare the character arrays we need in our password
# Presented as characters to facilitate string concatenation

DIGITS = [ `0` , `1` , `2` , ` 3` , `4` , ` 5` , `6` , ` 7` , `8` , ` 9`

LOCASE_CHARACTERS = [ `a` , ` b` , `c` , `d ` , ` e` , `f` , ` g` , `h`

  `i` , ` j` , `k` , ` m` , `n` , ` o ` , ` p` , `q` ,

  ` r` , `s` , `t`  , `u` , ` v` , `w` , `x` , ` y` ,

`z` ]

 

UPCASE_CHARACTERS = [ `A` , ` B` , `C` , `D` , ` E` , ` F` , `G` , ` H`

` I` , `J` , `K` , ` M ` , ` N` , `O` , ` p` , `Q` ,

  `R` , ` S` , `T` , ` U` , `V` , ` W ` , `X` , ` Y` ,

  ` Z` ]

 

SYMBOLS = [ `@` , `#` , `$` , `%` , `=` , `:` , `? ` , ` .` , `/` , `|` ,  `~` , `& gt;`

`*` , `(` , `)` , ` & lt; & amp; # 039;]

 
# concatenates all the character arrays above to form one array

COMBINED_LIST = DIGITS + UPCASE_CHARACTERS + LOCASE_CHARACTERS + SYMBOLS

 
# randomly select at least one character from each set pa characters above

rand_digit = random. choice (DIGITS)

rand_upper = random .choice (UPCASE_CHARACTERS)

rand_lower = random.choice (LOCASE_CHARACTERS)

rand_symbol = random.choice (SYMBOLS)

 
# concatenate a character randomly selected above
# at this point the password contains only 4 characters, but
# we want a 12-digit password

temp_pass = rand_digit + rand_up per + rand_lower + rand_symbol

  

  
# now we are sure that we have at least one character from each
# character set, fill in the rest
# password length by random selection from combined
# list of characters above.

for x in range (MAX_LEN - 4 ):

  temp_pass = temp_pass + random.choice (COMBINED_LIST)

# convert the temporary password to an array and shuffle it into

# prevent it so that it has a consistent pattern

# where the beginning of the password is predictable

temp_pass_list = array.array (& amp; # 039; u 039;, temp_pass)

random.shuffle (temp_pass_list)

 
# go through an array of temporary passwords and add characters
# generate password

password = " "

for x in temp_pass_list:

password = password + x

 
# print password

print (password)

< / code>

Output:

 yf2byU $ @ zTg5