0

我做了一个小程序,它会询问你的密码和用户名。输入详细信息后,它应该检查密码和用户名是否正确。我该如何接近并做到这一点?

from tkinter import *
from getpass import getpass

def callback():
    print(E1)()

top = Tk()
L1 = Label(top, text="User Name")
L1.grid(row=0, column=0)
E1 = Entry(top, bd = 5)
E1.grid(row=0, column=1)

L1 = Label(top, text="Password")
L1.grid(row=1, column=0)
E1 = Entry(top, bd = 5,show="•")
E1.grid(row=1, column=1)

MyButton1 = Button(top, text="Submit", width=10, command=callback)
MyButton1.grid(row=3, column=1)

top.mainloop()
4

1 回答 1

1

这里有一些代码演示了 getpass 的使用以及如何检查用户提供的密码和散列密码。这忽略了许多问题,例如加盐哈希、存储身份验证数据的适当位置、您需要支持多少用户等。

import getpass, hashlib

USER = 'ali_baba'
# hashlib.md5('open sesame').hexdigest()
PASSWORD_HASH = '54ef36ec71201fdf9d1423fd26f97f6b'

user = raw_input("Who are you? ")
password = getpass.getpass("What's the password? ")
password_hash = hashlib.md5(password).hexdigest()

if (user == USER) and (password_hash == PASSWORD_HASH):
    print "user authenticated"
else:
    print "user authentication failed"

如果您不想将用户名存储在代码中,可以这样做:

# hashlib.md5('ali_baba:open sesame').hexdigest()
AUTH_HASH = '0fce635beba659c6341d76da4f97212f'
user = raw_input("Who are you? ")
password = getpass.getpass("What's the password? ")
auth_hash = hashlib.md5('%s:%s' % (user, password)).hexdigest()
if auth_hash == AUTH_HASH:
    print "user authenticated"
else:
    print "user authentication failed"
于 2012-06-12T04:09:21.727 回答