Storing Username And Password Into A Dictionary?
Solution 1:
you dont ... you save a hash into a dictionary (a hash is simpley a non reversable encoding)
eg:
md5("password") == '5f4dcc3b5aa765d61d8327deb882cf99'
however there is no real way to go from that back to the password
nothing_does_this('5f4dcc3b5aa765d61d8327deb882cf99') == "password"
(not entirely true... but close enough fo the concept)
import hashlib
defcreate_users()
users = {}
whileTrue:
username = raw_input("Enter Username:")
password = raw_input("Enter Password:")
users[username] = hashlib.md5(password).hexdigest()
if raw_input("continue?")[0].lower() != "y":
return users
deflogin(userdict):
username = raw_input("Username:")
password = raw_input("Password:")
return userdict.get(username,None) == hashlib.md5(password).hexdigest()
users = create_users()
if login(users):
print"Double Winning!!!"else:
print"You Lose Sucka!!!"
as pointed out md5 is not a very secure hash, there are much better ones to use sha256 is pretty good still I think, bcrypt is even better (for some definition of better) ... however md5 is a simple hash to help with understanding what they are..
Solution 2:
If you already have constructed this dictionary, you can save it to a file with pickle.
pickle.dump( user_dict, open( "save.p", "wb" ) )
However, you should be aware of best practices when storing passwords and make sure you are storing a securely hashed version of the password rather than its value in plaintext.
Post a Comment for "Storing Username And Password Into A Dictionary?"