11import argparse # to work with the arguments
22import string
3- import random
3+ import secrets
44
55
66# function to generate the password
@@ -15,26 +15,26 @@ def get_password(length: int) -> str:
1515 password = ''
1616
1717 # adding one symbol
18- password += random .choice (symbols )
18+ password += secrets .choice (symbols )
1919
2020 # adding one number
21- password += random .choice (numbers )
21+ password += secrets .choice (numbers )
2222
2323 # adding one lowercase character
24- password += random .choice (lowercase_characters )
24+ password += secrets .choice (lowercase_characters )
2525
2626 # adding one uppercase character
27- password += random .choice (uppercase_characters )
27+ password += secrets .choice (uppercase_characters )
2828
2929 # now run a for loop starting from the current length of the password
3030 # all the way to the maxium length to fill in the remaining data
3131 while len (password ) < length :
3232 # adding all the data together into a one list
33- characters = list ( lowercase_characters ) + list ( uppercase_characters )
34- data = symbols + numbers + characters
33+ characters = lowercase_characters + uppercase_characters
34+ data = symbols + numbers + list ( characters )
3535
3636 # getting a random character from the list
37- random_char = random .choice (data )
37+ random_char = secrets .choice (data )
3838
3939 # if asked to exclude duplicates
4040 if args .excludeduplicates :
@@ -46,24 +46,39 @@ def get_password(length: int) -> str:
4646 # then just add the character without checking
4747 password += random_char
4848
49- # shuffling the generated password to mix every character
50- # creating a list of the password
49+ # create a list of the password
5150 password_list = list (password )
52- random .shuffle (password_list ) # shuffling the list
51+ # shuffle the list into random sequence
52+ password = shuffle (password_list )
5353
5454 # returning the password
55- return '' .join (password_list )
56-
55+ return password
56+
57+
58+ # shuffle function based on Fisher–Yates shuffle using secrets.choice()
59+ # as the integer selector
60+ def shuffle (password : list ):
61+ # n used to determine range of loop
62+ n = len (password )
63+ for x in range (n - 1 , 0 , - 1 ):
64+ # set new variable y to random int within needed index
65+ y = secrets .choice (range (0 , x + 1 ))
66+ # swap elements at index x and index y
67+ password [x ], password [y ] = password [y ], password [x ]
68+ # return concatenated password
69+ return '' .join (password )
5770
5871# main method
72+
73+
5974def main (args : argparse .Namespace ) -> None :
6075 # storing the length in a variable
6176 length = args .length
6277
6378 # if the value is out of range then inform the user and exit
64- if length < 6 or length > 20 :
65- print ('ERROR: -l/--length should be in the range of 6 - 20' )
66- exit (0 )
79+ # if length < 6 or length > 20:
80+ # print('ERROR: -l/--length should be in the range of 6 - 20')
81+ # exit(0)
6782
6883 # this will hold the final password
6984 password = get_password (length )
0 commit comments