【问题标题】:Check if password and username input match in Python?检查Python中的密码和用户名输入是否匹配?
【发布时间】:2012-06-12 03:01:54
【问题描述】:

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

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

【问题讨论】:

  • 您正在导入 getpass 模块,但您没有使用它。你想知道怎么用,还是想用自己的Tk代码?
  • 那么,您在哪里以及以什么形式存储正确的密码或正确的密码哈希?您需要通过某种方式从您的程序中访问该信息。
  • 是的,我想知道如何使用 getpass,我正在考虑将密码和用户名存储在代码中的某个地方。

标签: python tkinter tkinter-entry


【解决方案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"

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2020-06-18
    相关资源
    最近更新 更多