I made a password generator to teach Python3 to children (bonus) * Completely remade

It seems that changing the PIN of my number card is popular, so I made a password generator Update: 2020/05/30

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.

Password generator specifications

  1. Password with any number of digits
  2. Mix (or choose) lowercase English letters
  3. Mix (or choose) English capital letters
  4. Mix (or select) symbols
  5. Repel 3 consecutive characters (123, abc, 987, XYZ, 121, etc. are not good!)
  6. Make sure that the symbol is included when it is included.
  7. Make it possible to exclude symbols, 0s, O, and other things that are difficult to enter or that are likely to be mistaken (the characters shown in the example include characters that I dislike as passwords).
  8. Create the specified number of passwords for the above rules
  9. Output to screen

A little commentary

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

Recommended Posts

I made a password generator to teach Python3 to children (bonus) * Completely remade
I made Othello to teach Python3 to children (4)
I made Othello to teach Python3 to children (2)
I made Othello to teach Python3 to children (5)
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made Othello to teach Python3 to children (6) Final episode
I made a Python module to translate comment outs
I tried to automatically generate a password with Python3
I made a python library to do rolling rank
I made a python text
I made a package to filter time series with python
I made a Line-bot using Python!
I made a fortune with Python.
I made a daemon with Python
I made a library to easily read config files with Python
I made a library that adds docstring to a Python stub file.
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
I made a payroll program in Python!
I made a character counter with Python
I want to build a Python environment
I made a script to display emoji
I made a Hex map with Python
I made a stamp generator with GAN
After studying Python3, I made a Slackbot
I created a password tool in Python.
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
Python: I tried to make a flat / flat_map just right with a generator
[Python] I made a decorator that doesn't seem to have any use.
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a web application in Python that converts Markdown to HTML
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a script in python to convert .md files to Scrapbox format
I made a program to check the size of a file in Python
I made a function to see the movement of a two-dimensional array (Python)
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
I want to create a window in Python
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
I tried to explain what a Python generator is for as easily as possible.
[Python] I made a Youtube Downloader with Tkinter.
I made a tool to compile Hy natively
I made a module in C language to filter images loaded by Python
I made a tool to get new articles
I made a script to record the active window using win32gui of Python
I want to write to a file with Python
A story that I was addicted to when I made SFTP communication with python
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a program to convert images into ASCII art with Python and OpenCV
[Python] I made a system to introduce "recipes I really want" from the recipe site!