我差不多完成了我的gui。这应该检查输入的密码的强度,即它有多长,大小写,特殊字符等... 将该密码哈希到md5哈希中,将其存储在文本文件中。然后用户将重新输入密码,将进行重新散列,然后检查文本文件以查看该散列是否在那里。但是我似乎无法正确地重新输入密码并使用它来检查文件。
我的完整代码:
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')
guiFont2 = font1 = dict(family='Courier New, monospaced', size=18, color='9400d3')
#====== 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) == 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, font=guiFont2)
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, font=guiFont2)
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 ======
def verifyHash():
hashinput = vHash.get()
hashobj2 = hashlib.md5(hashinput.encode('utf-8')).hexidigest()
with open('password_hash_log.txt') as obj3:
for line in obj3:
line = line.rstrip()
if line == hashobj2:
output.set("Password Match")
else:
output.set("Passwords do not match try again")
output = StringVar()
lblVerify = Label(myGui, text="Enter Password to Verify: ", font=guiFont)
lblVerify.grid(row=5, column=0, sticky=W)
vHash = Entry(myGui, show="*")
vHash.grid(row=5, column=1)
vBtn = Button(myGui, text="Verify Password", command=verifyHash, height=2, width=25, font=guiFont)
vBtn.grid(row=6, column=0)
vLbl = Label(myGui, textvariable=output, font=guiFont2)
vLbl.grid(row=6, column=1, sticky=W)
myGui.mainloop()
我已经接近完成了我需要做的事情,所以非常感谢任何帮助。
答案 0 :(得分:0)
在我之前的例子中,没有通过hexidigest传递任何东西来产生哈希。我修改了代码的最后部分,gui现在按计划工作。
新代码:
def verifyHash():
hash_obj2 = hashlib.md5()
pwmd52 = vHash.get().encode('utf-8')
hash_obj2.update(pwmd52)
md5pw2.set(hash_obj2.hexdigest())
with open('password_hash_log.txt') as obj3:
for line in obj3:
line = line.rstrip()
if line == md5pw2.get():
output.set("Password Match")
else:
output.set("Passwords do not match try again")
md5pw2 = StringVar()
现在md5pw2是Hashed代码并验证Hash接受并检查文本文件的每一行以查看该Hash是否在那里,然后如果找到它则返回密码匹配。