2012-06-12 67 views
0

我已經做了一個小程序,應該問你的密碼和用戶名。輸入您的詳細資料後,應檢查密碼和用戶名是否正確。我如何接近並做到這一點?檢查Python中的密碼和用戶名輸入是否匹配?

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

您正在導入getpass模塊,但您沒有使用它。你想知道如何使用它,或者你想使用自己的Tk代碼嗎? – jedwards

+0

那麼,你在哪裏和以什麼形式存儲正確的密碼或正確的密碼哈希?您需要某種方式從程序中訪問這些信息。 –

+0

是的,我想知道如何使用getpass,我正在考慮在代碼中存儲密碼和用戶名。 – EatMyApples

回答

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" 
相關問題