Actually, I needed a password with any number of digits and 1000 or more for work ... Since it is a good idea, I also studied Python and made a password generator with the following specifications.
You haven't done much. Changed to weight symbols in random.choices (population, weights = weights, k = password_length))
. (Because I felt that the symbol Osugi in the password!) Weighting is done with make_weights (population)
, so if you think it is unbalanced, please change this.
From the perspective of "teaching children," this program is more difficult than Othello. Because the concept of character code comes out. It's hard to understand even if you say, "A displayed on the screen you are watching is a hexadecimal number 0x41."
# Password generator
# created 2020 (C) tsFox
import random , string , re
class password_generator_class:
def __init__(self):
self.punctuation = "!""#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
#Keyboard input
def yndinput(self,yndstr,yndsw):
while True:
akey = input(yndstr)
if yndsw and akey.isdigit():
return akey
if yndsw == False and ( akey.lower() == 'n' or akey.lower() == 'y' ):
return akey.lower()
#Enter the characters you want to exclude and return a new population
def remove_specific_characters(self,population):
yn = pg.yndinput("Are there any characters you want to exclude?(y/n)=", False)
if yn == 'n':
return population
#Specify excluded characters
while True:
specific_characters = input("Enter all the characters you don't want to use (eg O`\\'l)=")
if specific_characters != "":
break
#List once and erase unnecessary symbols
list_population = list(population)
for c in specific_characters:
list_population.remove(c)
#Also return to the string
new_population = "".join(list_population)
return new_population
#Create a weight
def make_weights(self,current_population):
list_weights = list()
for c in current_population:
list_weights.append(2 if c in self.punctuation else 6)
return list_weights
#Password validity
def check_validity(self,tmp_password,punctation_check):
for x in range(len(tmp_password)-2):
a = ord(tmp_password[x])
b = ord(tmp_password[x+1])
c = ord(tmp_password[x+2])
if ( -1 <= int(a - b) <= 1 ) and ( -1 <= int(b - c) <= 1 ):
return 1
start_punctation = re.search( "[{}]".format(self.punctuation) ,tmp_password)
if punctation_check == "y" and start_punctation == None:
return -1
return 0
if __name__ == '__main__':
#Main logic
pg = password_generator_class()
passwords =list()
password = str()
weights = list()
specific_characters = str()
#If not specified, only numbers
population = string.digits
generate_count = int(pg.yndinput("Number of passwords to generate(Numerical value)=",True))
password_length = int(pg.yndinput("Number of digits in password(Numerical value)=",True))
if pg.yndinput("Do you include lowercase English?(y/n)=", False) == 'y':
population = population + string.ascii_lowercase
if pg.yndinput("Do you include English capital letters?(y/n)=", False) == 'y':
population = population + string.ascii_uppercase
#Whether to put a symbol
punctation_yn = pg.yndinput("Do you include the symbol?(y/n)=", False)
if punctation_yn == "y":
population = population + string.punctuation
#Remove any excluded characters and put them in a new population
population = pg.remove_specific_characters(population)
#Reduce symbol weighting
weights = pg.make_weights(population)
#Loop for the number of generations
for n in range(generate_count):
#Loop until a usable password is created
while True:
password = "".join(random.choices(population,weights=weights,k=password_length))
if pg.check_validity(password,punctation_yn):
continue
#double check
if password not in passwords:
break
#Add password
passwords.append(password)
#Display on screen
print(*passwords , sep = "\n" )
That's why we have released the source of the password generator. Let's meet again! !!
c u