Password Generator Using by Python Script

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
ankitdixit
Posts: 3
Joined: Tue Jun 08, 2021 7:50 am

Password Generator Using by Python Script

Post by ankitdixit » Tue Jul 27, 2021 6:56 am

Hello All, I am working on a Password Generator Python Project and I want to know the provided sample code is correct or not? I have taken this source code on Interviewbit and in this python project, the most difficult part of managing multiple accounts is generating a different strong password for each. A strong password is a mix of alphabets, numbers, and alphanumeric characters. Therefore, the best use of Python could be building a project where you could generate random passwords for any of your accounts. Can anyone tell me, Is it right or provide some other source code for the number guessing project.

Code: Select all

import random

def generatePassword(pwlength):

    alphabet = "abcdefghijklmnopqrstuvwxyz"

    passwords = [] 

    for i in pwlength:
        
        password = "" 
        for j in range(i):
            next_letter_index = random.randrange(len(alphabet))
            password = password + alphabet[next_letter_index]
        
        password = replaceWithNumber(password)
        password = replaceWithUppercaseLetter(password)
        
        passwords.append(password) 
    
    return passwords


def replaceWithNumber(pword):
    for i in range(random.randrange(1,3)):
        replace_index = random.randrange(len(pword)//2)
        pword = pword[0:replace_index] + str(random.randrange(10)) + pword[replace_index+1:]
        return pword


def replaceWithUppercaseLetter(pword):
    for i in range(random.randrange(1,3)):
        replace_index = random.randrange(len(pword)//2,len(pword))
        pword = pword[0:replace_index] + pword[replace_index].upper() + pword[replace_index+1:]
        return pword

def main():
    
    numPasswords = int(input("How many passwords do you want to generate? "))
    
    print("Generating " +str(numPasswords)+" passwords")
    
    passwordLengths = []

    print("Minimum length of password should be 3")

    for i in range(numPasswords):
        length = int(input("Enter the length of Password #" + str(i+1) + " "))
        if length<3:
            length = 3
        passwordLengths.append(length)
    
    
    Password = generatePassword(passwordLengths)

    for i in range(numPasswords):
        print ("Password #"+str(i+1)+" = " + Password[i])



main()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Password Generator Using by Python Script

Post by pythoncoder » Tue Jul 27, 2021 8:11 am

That solution is a little complicated. With some understanding of the ASCII character set, you could do something like this:

Code: Select all

import random

def randchar():
    while True:
        yield chr(48 + (random.getrandbits(30) % 75))

r = randchar()

def random_string(length):
    return ''.join((next(r) for _ in range(length)))
which can be tested thus:

Code: Select all

# Test
for _ in range(10):
    print(random_string(20))
producing (in my case)

Code: Select all

DS7`@jITN^BlGLu`KdKQ
RA6=sCdovp:ptCgV2dwb
VS7mD9Ao@8C?pyubWD:G
_H;oftGbtN`5=lQcT`6<
fo9CP:A\IwMQgivJ=64h
;N2[hN45\n8cQB0Ay^Mm
I3aaLjHSHe0lOn\J]XJ0
__8k4V@QE;j?]sIu;TDg
o;DT`2\v^r`be<XlNCBq
XkQuF\PQ7yx=gBHk@7OP
Peter Hinch
Index to my micropython libraries.

satineeraj
Posts: 5
Joined: Fri Aug 21, 2020 4:23 am

Re: Password Generator Using by Python Script

Post by satineeraj » Sun Jul 10, 2022 8:27 am

Hi, @ankitdixit you can check this:

Code: Select all

import string
import random

#all characters
characters =  string.ascii_letters + string.digits + string.punctuation

#size of the password
size = 10

#generate a random password using Python
random_password = ""

for i in range(size):
    #pick a random character from characters
    character = random.choice(characters)

    #add the random pick character to the random password
    random_password += character

print("Your Random password is:",random_password)
from this code you can generate a random password, first, compile this code, I found this code here, and hope this will help you.

satineeraj
Posts: 5
Joined: Fri Aug 21, 2020 4:23 am

Re: Password Generator Using by Python Script

Post by satineeraj » Fri Nov 11, 2022 8:41 am

Hi,

you can check this code:

Code: Select all

import random
import pyperclip
from tkinter import *
from tkinter.ttk import *
 
# Function for calculation of password
 
 
def low():
    entry.delete(0, END)
 
    # Get the length of password
    length = var1.get()
 
    lower = "abcdefghijklmnopqrstuvwxyz"
    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !@#$%^&*()"
    password = ""
 
    # if strength selected is low
    if var.get() == 1:
        for i in range(0, length):
            password = password + random.choice(lower)
        return password
 
    # if strength selected is medium
    elif var.get() == 0:
        for i in range(0, length):
            password = password + random.choice(upper)
        return password
 
    # if strength selected is strong
    elif var.get() == 3:
        for i in range(0, length):
            password = password + random.choice(digits)
        return password
    else:
        print("Please choose an option")
 
 
# Function for generation of password
def generate():
    password1 = low()
    entry.insert(10, password1)
 
 
# Function for copying password to clipboard
def copy1():
    random_password = entry.get()
    pyperclip.copy(random_password)
 
 
# Main Function
 
# create GUI window
root = Tk()
var = IntVar()
var1 = IntVar()
 
# Title of your GUI window
root.title("Random Password Generator")
 
# create label and entry to show
# password generated
Random_password = Label(root, text="Password")
Random_password.grid(row=0)
entry = Entry(root)
entry.grid(row=0, column=1)
 
# create label for length of password
c_label = Label(root, text="Length")
c_label.grid(row=1)
 
# create Buttons Copy which will copy
# password to clipboard and Generate
# which will generate the password
copy_button = Button(root, text="Copy", command=copy1)
copy_button.grid(row=0, column=2)
generate_button = Button(root, text="Generate", command=generate)
generate_button.grid(row=0, column=3)
 
# Radio Buttons for deciding the
# strength of password
# Default strength is Medium
radio_low = Radiobutton(root, text="Low", variable=var, value=1)
radio_low.grid(row=1, column=2, sticky='E')
radio_middle = Radiobutton(root, text="Medium", variable=var, value=0)
radio_middle.grid(row=1, column=3, sticky='E')
radio_strong = Radiobutton(root, text="Strong", variable=var, value=3)
radio_strong.grid(row=1, column=4, sticky='E')
combo = Combobox(root, textvariable=var1)
 
# Combo Box for length of your password
combo['values'] = (8, 9, 10, 11, 12, 13, 14, 15, 16,
                   17, 18, 19, 20, 21, 22, 23, 24, 25,
                   26, 27, 28, 29, 30, 31, 32, "Length")
combo.current(0)
combo.bind('<<ComboboxSelected>>')
combo.grid(column=1, row=1)
 
# start the GUI
root.mainloop()
and i also found this post on the Internet about the Python Projects and i get this code, hope this will help you.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Password Generator Using by Python Script

Post by TheSilverBullet » Fri Nov 11, 2022 11:06 am

Code: Select all

#!/usr/bin/python3
import random 
dataset = []
dataset.extend([chr(x) for x in range(ord('0'),ord('9')+1)])
dataset.extend([chr(x) for x in range(ord('A'),ord('Z')+1)])
dataset.extend([chr(x) for x in range(ord('a'),ord('z')+1)])
pw = ''.join([dataset[random.randrange(0,len(dataset))] for _ in range(20)])
print(pw)
>>> EzUAgobxZway3ZmfYbd7
Just as a suggestion…

Post Reply