【问题标题】:Python tkinter password strength checker guiPython tkinter 密码强度检查器 gui
【发布时间】:2016-12-04 13:54:01
【问题描述】:

我正在尝试创建一个密码强度检查器 gui,它检查密码的长度(超过 8 个字符)小写和大写字母、数字和特殊字符,然后告诉你它是什么级别、弱、强等... . 然后它会创建一个 md5 哈希并显示它,然后您可以将此哈希存储在一个文本文件中。然后再次重新输入密码并从文本文件中验证(尚未为此编写任何代码)。

我已经设法实现了强度检查、哈希生成、记录到文件....我想。虽然如果没有输入密码,我希望代码返回“密码不能为空”,但它似乎在 gui 中不起作用。相同的代码在 shell 中工作。即使仅使用 3 个字符作为密码,该代码也永远不会返回“非常弱”作为强度。

到目前为止,这是我的代码:

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()

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    您有 checkPassword 函数的结果,其中密码少于 4 或 1 个字符,作为返回语句返回。您没有为 checkPassword 的结果分配变量,因此在这两种情况下无法从函数接收数据。我建议更像:

    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()
    

    【讨论】:

    • 我已经为此挠头很久了,谢谢。我已经为我自己的代码删除了 checkPassword 中的打印,因为我猜这是用来测试它的。
    • 糟糕,抱歉。确实是 :) 我打算删除它。
    猜你喜欢
    • 2016-12-04
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2014-10-26
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多