前提
他人のpythonのコードを改造してパスワードジェネレーターを作っています。
記号、数字、アルファベットそれぞれのオンオフ機能を追加したいのですが、特にエラーもでてないのにうまくいきません。
試したこと
random_char = secrets.choice(data)から
data = 〜〜でパスワードの文字を管理してると思ったので、分岐によってdataに記号や数字を渡さないプログラムを書きました。
ちゃんと分岐できたか確かめるprint文を書き、しっかり分岐していることがわかりました。
elseはなにも制限してない分岐ですが、実行の結果からelseのprintが表示されてないので、この分岐は通ってないと思われます。
それなのになぜ、結果からは制限されてないパスワードが生成されるのでしょうか?
実行の結果
-es記号、 -en数字、 -eaアルファベットをそれぞれ制限する。 $ python3 password_generator.py -es Hello, symbols Hello, symbols PASSWORD: Wx4-N2 $ python3 password_generator.py -en Hello, numbers Hello, numbers PASSWORD: k4$g$K $ python3 password_generator.py -ea Hello, alphabets Hello, alphabets PASSWORD: G03e)(
該当のソースコード
python
1# functions my want to achieve 2# 1.you can selected symbols on/off 3# 2.you can selected numbers on/off 4# 3.you can selected alphabet on/off 5# 4.auto copy , achieved in 11/24 6# 5.make a error what use -l more 74 and -ed in 11/24 7 8import argparse # to work with the arguments 9import string 10import secrets 11import pyperclip # auto copy moduel 12 13 14# function to generate the password 15def get_password(length: int) -> str: 16 # creating data 17 symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+'] 18 # symbols = ['!', '#', '$', '%', '&', ' (', ')','*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~'] 19 numbers = [str(a) for a in range(10)] 20 lowercase_characters = string.ascii_lowercase 21 uppercase_characters = string.ascii_uppercase 22 23 # variable to hold the password 24 password = '' 25 26 # adding one symbol 27 password += secrets.choice(symbols) 28 29 # adding one number 30 password += secrets.choice(numbers) 31 32 # adding one lowercase character 33 password += secrets.choice(lowercase_characters) 34 35 # adding one uppercase character 36 password += secrets.choice(uppercase_characters) 37 38 # now run a for loop starting from the current length of the password 39 # all the way to the maxium length to fill in the remaining data 40 while len(password) < length: 41 # adding all the data together into a one list 42 characters = lowercase_characters + uppercase_characters 43 if args.excludesymbols: 44 data = numbers + list(characters) 45 print('Hello, symbols') 46 elif args.excludenumbers: 47 data = symbols + list(characters) 48 print('Hello, numbers') 49 elif args.excludealphabet: 50 data = symbols + numbers 51 print('Hello, alphabets') 52 else: 53 data = symbols + numbers + list(characters) 54 print('Hello.') 55 56 # getting a random character from the list 57 random_char = secrets.choice(data) 58 59 # if asked to exclude duplicates 60 if args.excludeduplicates: 61 # if character not already inside the password 62 # then add it into the password 63 if random_char not in password: 64 password += random_char 65 else: # if not asked to exclude duplicates 66 # then just add the character without checking 67 password += random_char 68 69 # create a list of the password 70 password_list = list(password) 71 # shuffle the list into random sequence 72 password = shuffle(password_list) 73 74 # returning the password 75 return password 76 77 78# shuffle function based on Fisher–Yates shuffle using secrets.choice() 79# as the integer selector 80def shuffle(password: list): 81 # n used to determine range of loop 82 n = len(password) 83 for x in range(n - 1, 0, -1): 84 # set new variable y to random int within needed index 85 y = secrets.choice(range(0, x + 1)) 86 # swap elements at index x and index y 87 password[x], password[y] = password[y], password[x] 88 # return concatenated password 89 return ''.join(password) 90 91# main method 92 93 94def main(args: argparse.Namespace) -> None: 95 # storing the length in a variable 96 length = args.length 97 98 # if the value is out of range then inform the user and exit 99 # if length < 6 or length > 20: 100 # print('ERROR: -l/--length should be in the range of 6 - 20') 101 # exit(0) 102 103 # add 104 if length > 74 and args.excludeduplicates: 105 print("ERROR: its too many length") 106 exit(0) 107 108 # this will hold the final password 109 password = get_password(length) 110 111 # printing the password to the user 112 print(f'PASSWORD: {password}') 113 pyperclip.copy(password) 114 115 116if __name__ == '__main__': 117 # creating the argparse object 118 parser = argparse.ArgumentParser() 119 120 # adding the length argument 121 parser.add_argument( 122 '-l', '--length', 123 help='Length of the password. [MIN=1], [MAX=74, if you use -l and -ed ], [DEFAULT = 6]', 124 metavar='', 125 default=6, 126 type=int 127 ) 128 129 # adding the exclude duplicates argument 130 parser.add_argument( 131 '-ed', 132 '--excludeduplicates', 133 action='store_true', 134 help='Excludes duplicate characters from the password' 135 ) 136 137 parser.add_argument( 138 '-es', 139 '--excludesymbols', 140 action='store_true', 141 help='Excludes symbols characters from the password' 142 ) 143 144 parser.add_argument( 145 '-en', 146 '--excludenumbers', 147 action='store_true', 148 help='excludes numbers characters from the password' 149 ) 150 151 parser.add_argument( 152 '-ea', 153 '--excludealphabet', 154 action='store_true', 155 help='excludes alphabet characters from the password' 156 ) 157 158 # parsing the argument 159 args = parser.parse_args() 160 161 # calling the main method with the args 162 main(args) 163
その他の情報
UbuntuとEmacsで書いてます
python は3.9.6です
回答1件
あなたの回答
tips
プレビュー