2016-07-29 11 views
1

Ich versuche, ein Passwort Stärke Checker GUI, die ein Passwort für die Länge (mehr als 8 Zeichen) Kleinbuchstaben und Großbuchstaben, Zahlen und Sonderzeichen überprüft und dann sagt Ihnen, was es ist, schwach , stark etc .... Es erstellt dann einen MD5-Hash und zeigt dies, dann können Sie diesen Hash in einer Textdatei speichern. Dann gib das Passwort erneut ein und verifiziere es aus der Textdatei (noch keinen Code dafür gemacht).Python tkinter Passwort Stärke Checker gui

Ich habe es geschafft, die Stärke zu überprüfen, Hash-Generation, in eine Datei loggen .... Ich denke. Obwohl, wenn kein Passwort eingegeben wird, möchte ich, dass der Code "Passwort kann nicht leer sein", aber es scheint nicht in einer GUI zu arbeiten. Derselbe Code funktioniert innerhalb einer Shell. Der Code gibt auch niemals "sehr schwach" als Stärke zurück, selbst wenn nur 3 Zeichen als Passwort verwendet werden.

Hier ist mein Code so weit:

from tkinter import * 
import hashlib 
import os 
import re 

myGui = Tk() 
myGui.geometry('500x400+700+250') 
myGui.title('Password Generator') 
guiFont = font = dict(family='Courier New, monospaced', size=18, color='#7f7f7f') 


#====== Password Entry ========== 
eLabel = Label(myGui, text="Please Enter you Password: ", font=guiFont) 
eLabel.grid(row=0, column=0) 
ePassword = Entry(myGui, show="*") 
ePassword.grid(row=0, column=1) 



#====== Strength Check ======= 


def checkPassword(): 
    strength = ['Password can not be Blank', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'] 
    score = 1 
    password = ePassword.get() 

    if len(password) < 1: 
     return strength[0] 

    if len(password) < 4: 
     return strength[1] 

    if len(password) >= 8: 
     score += 1 

    if re.search("[0-9]", password): 
     score += 1 

    if re.search("[a-z]", password) and re.search("[A-Z]", password): 
     score += 1 

    if re.search(".", password): 
     score += 1 

    passwordStrength.set(strength[score]) 

passwordStrength = StringVar() 
checkStrBtn = Button(myGui, text="Check Strength", command=checkPassword, height=2, width=25, font=guiFont) 
checkStrBtn.grid(row=2, column=0) 
checkStrLab = Label(myGui, textvariable=passwordStrength) 
checkStrLab.grid(row=2, column=1, sticky=W) 

#====== Hash the Password ====== 


def passwordHash(): 
    hash_obj1 = hashlib.md5() 
    pwmd5 = ePassword.get().encode('utf-8') 
    hash_obj1.update(pwmd5) 
    md5pw.set(hash_obj1.hexdigest()) 

md5pw = StringVar() 
hashBtn = Button(myGui, text="Generate Hash", command=passwordHash, height=2, width=25, font=guiFont) 
hashBtn.grid(row=3, column=0) 
hashLbl = Label(myGui, textvariable=md5pw) 
hashLbl.grid(row=3, column=1, sticky=W) 


#====== Log the Hash to a file ======= 


def hashlog(): 
    loghash = md5pw.get() 

    if os.path.isfile('password_hash_log.txt'): 
     obj1 = open('password_hash_log.txt', 'a') 
     obj1.write(loghash) 
     obj1.write("\n") 
     obj1.close() 

    else: 
     obj2 = open('password_hash_log.txt', 'w') 
     obj2.write(loghash) 
     obj2.write("\n") 
     obj2.close() 

btnLog = Button(myGui, text="Log Hash", command=hashlog, height=2, width=25, font=guiFont) 
btnLog.grid(row=4, column=0) 

#====== Re enter password and check against stored hash ====== 
lblVerify = Label(myGui, text="Enter Password to Verify: ", font=guiFont) 
lblVerify.grid(row=5, column=0, sticky=W) 

myGui.mainloop() 

Jede Hilfe sehr geschätzt werden würde. Vielen Dank.

Antwort

1

Sie haben die Ergebnisse der checkPassword-Funktion, in der die Kennwörter entweder weniger als 4 oder 1 Zeichen enthalten, als Rückgabeanweisungen zurückgegeben. Sie haben dem Ergebnis von checkPassword keine Variable zugewiesen, sodass in diesen beiden Fällen keine Daten von der Funktion empfangen werden können. Ich würde vorschlagen, etwas mehr wie:

from tkinter import * 
import hashlib 
import os 
import re 

myGui = Tk() 
myGui.geometry('500x400+700+250') 
myGui.title('Password Generator') 
guiFont = font = dict(family='Courier New, monospaced', size=18, color='#7f7f7f') 


#====== Password Entry ========== 
eLabel = Label(myGui, text="Please Enter you Password: ", font=guiFont) 
eLabel.grid(row=0, column=0) 
ePassword = Entry(myGui, show="*") 
ePassword.grid(row=0, column=1) 



#====== Strength Check ======= 


def checkPassword(): 
    strength = ['Password can not be Blank', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'] 
    score = 1 
    password = ePassword.get() 
    print password, len(password) 

    if len(password) == 0: 
     passwordStrength.set(strength[0]) 
     return 

    if len(password) < 4: 
     passwordStrength.set(strength[1]) 
     return 

    if len(password) >= 8: 
     score += 1 

    if re.search("[0-9]", password): 
     score += 1 

    if re.search("[a-z]", password) and re.search("[A-Z]", password): 
     score += 1 

    if re.search(".", password): 
     score += 1 

    passwordStrength.set(strength[score]) 

passwordStrength = StringVar() 
checkStrBtn = Button(myGui, text="Check Strength", command=checkPassword, height=2, width=25, font=guiFont) 
checkStrBtn.grid(row=2, column=0) 
checkStrLab = Label(myGui, textvariable=passwordStrength) 
checkStrLab.grid(row=2, column=1, sticky=W) 

#====== Hash the Password ====== 


def passwordHash(): 
    hash_obj1 = hashlib.md5() 
    pwmd5 = ePassword.get().encode('utf-8') 
    hash_obj1.update(pwmd5) 
    md5pw.set(hash_obj1.hexdigest()) 

md5pw = StringVar() 
hashBtn = Button(myGui, text="Generate Hash", command=passwordHash, height=2, width=25, font=guiFont) 
hashBtn.grid(row=3, column=0) 
hashLbl = Label(myGui, textvariable=md5pw) 
hashLbl.grid(row=3, column=1, sticky=W) 


#====== Log the Hash to a file ======= 


def hashlog(): 
    loghash = md5pw.get() 

    if os.path.isfile('password_hash_log.txt'): 
     obj1 = open('password_hash_log.txt', 'a') 
     obj1.write(loghash) 
     obj1.write("\n") 
     obj1.close() 

    else: 
     obj2 = open('password_hash_log.txt', 'w') 
     obj2.write(loghash) 
     obj2.write("\n") 
     obj2.close() 

btnLog = Button(myGui, text="Log Hash", command=hashlog, height=2, width=25, font=guiFont) 
btnLog.grid(row=4, column=0) 

#====== Re enter password and check against stored hash ====== 
lblVerify = Label(myGui, text="Enter Password to Verify: ", font=guiFont) 
lblVerify.grid(row=5, column=0, sticky=W) 

myGui.mainloop() 
+0

Ich habe seit langem meinen Kopf mit diesem kratzen, danke. Ich habe den Ausdruck im checkPassword für meinen eigenen Code entfernt, da ich vermute, dass dies zum Testen verwendet wurde. – JSmith

+0

Hoppla, tut mir leid. In der Tat war es :) Ich wollte das entfernen. – RandomHash

Verwandte Themen