DEV Community

Cover image for secure and dynamic password generator with python
abdallah
abdallah

Posted on • Updated on

secure and dynamic password generator with python

this blog post is about creating a secure and dynamic Password generator, what I mean by dynamic is that you can create your own password pattern, take a look :

this is the pattern :

pattern.png

and this is the output :

terminal.png

look nice, isn't it? at least for me

so how this code works :

import secrets
import string
Enter fullscreen mode Exit fullscreen mode

first, let's talk about those modules :
string module contains all letters and numbers and symbols that we gonna use.

secrets is like random module but had more secure algorithms, the function we need is choice("here an iterable value")

#list of all characters supposed to be used in password

characters = string.ascii_letters
numbers = string.digits
symbols = string.punctuation

Enter fullscreen mode Exit fullscreen mode

so now those lists contain all possible characters for a password but the issue here is that it is an ordered list so for more security we gonna create 3 random lists to use for the password using the choice() method.

def create(PassType):
    if PassType == "word":
        randomCharacters=[]     
        for a in range(30):
                randomizeIndex = secrets.choice(range(len(characters)))
                randomedCharacter= characters[randomizeIndex]
                randomCharacters.append(randomedCharacter)
        return randomCharacters
    if PassType =="numbers":
        randomNumbers = []
        for a in range(20):
                randomNumbers.append(numbers[secrets.choice(range(len(numbers)))])
        return randomNumbers
    if PassType =="symbole" :
        randomSymbols = []
        for a in range(10):
                randomSymbols.append(symbols[secrets.choice(range(len(symbols)))])
        return randomSymbols

Enter fullscreen mode Exit fullscreen mode

like I said before choice method takes the list and chooses a secure random item from it, now we can create our new lists using this function.
note that in the loop you can take any number for the range method like for a in range(20) i chose 20 because I need a list of 20 items.
so let's create our new lists :

random_letters = create("word")
random_numbers= create("numbers")
random_punctuation =create("symbole")

Enter fullscreen mode Exit fullscreen mode

now let's create the function that validates the pattern and choose from those lists a password

def validatePattern():
    pattern_split = pattern.split(" ")
    passwordCharacters = []
    for character in pattern_split :
        if character =="s":
            passwordCharacters.append(random_punctuation[secrets.choice(range(len(random_punctuation)))])
        if character =="n":
            passwordCharacters.append(random_numbers[secrets.choice(range(len(random_numbers)))])
        if character =="l":
            passwordCharacters.append(random_letters[secrets.choice(range(len(random_letters)))])
    password= "".join(passwordCharacters)
    return password

Enter fullscreen mode Exit fullscreen mode

so here I set "l", "n" and "s" as characters used by pattern so now let's create our pattern and try it

pattern="l l l l l l l n n n s n"
password = validatePattern()
print(password)

Enter fullscreen mode Exit fullscreen mode

now we can change the pattern or create multiple passwords with the same pattern or even multiple patterns to multiple passwords, happy coding ...

my twitter : https://twitter.com/_81ab

code : https://github.com/abdallahkh/password-generator-python3

Top comments (0)