将用户名和密码存储到字典中?

[英]Storing username and password into a dictionary?


Currently at the moment im working a small login screen in python, What i want it to do is ask a user to register if theyre isnt a account already created, But one small problem im having is how can i save the username and password into a dictionary, I would also like to know how i would save the dictionary to a .txt file that can be loaded to restore user info/variables if this is possible.

目前我正在使用python中的一个小型登录屏幕,我想要它做的是要求用户注册他们是不是已经创建的帐户,但我遇到的一个小问题是如何将用户名和密码保存到字典,我还想知道我如何将字典保存为.txt文件,如果可能的话,可以加载该文件以恢复用户信息/变量。

Im not too sure whether im meant to ask for help with my code or im allowed to ask questions like this, I just need a reffernce or a little help. Also just to add im not asking for someone to do it for me, Just to give me a shove in the right direction

我不太确定我的意思是要求我的代码提供帮助,或者我允许提出这样的问题,我只需要一个reffernce或一点帮助。也只是为了添加我不要求别人为我做这件事,只是为了给我一个正确的方向推

Please dont flame me ;p

请不要激怒我; p

import sys
import math
user = None
password = None
store = dict()
newUser = True


while newUser == True:
    userguess=""
    passwordguess=""
    print("Hello, Please create a unique username and password.")
    user = input("USERNAME: ")
    password = input("PASSWORD: ")
    store[user] = password
    print(store)

That is what i have tried so far, I gathered the storing to dictionary from another page on here, Was just looking for a breakdown on assigning stuff to a key

这就是我到目前为止所尝试的,我在这里从另一个页面收集存储到字典,只是在寻找关键字来分配给一个键的东西

2 个解决方案

#1


4  

you dont ... you save a hash into a dictionary (a hash is simpley a non reversable encoding)

你没有...你将哈希保存到字典中(哈希是简单的不可逆编码)

eg: md5("password") == '5f4dcc3b5aa765d61d8327deb882cf99'

例如:md5(“password”)=='5f4dcc3b5aa765d61d8327deb882cf99'

however there is no real way to go from that back to the password

但是没有真正的方法可以从那回到密码

nothing_does_this('5f4dcc3b5aa765d61d8327deb882cf99') == "password"

nothing_does_this('5f4dcc3b5aa765d61d8327deb882cf99')==“密码”

(not entirely true... but close enough fo the concept)

(不完全正确......但与概念足够接近)

import hashlib
def create_users()
   users = {}
   while True:
      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

def login(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..

正如指出md5不是一个非常安全的哈希,有更好的使用sha256仍然相当不错我认为,bcrypt甚至更好(对于一些更好的定义)...但是md5是一个简单的哈希来帮助理解它们是什么..

#2


3  

If you already have constructed this dictionary, you can save it to a file with pickle.

如果您已经构建了此字典,则可以将其保存到带有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.

但是,在存储密码时应注意最佳实践,并确保存储密码的安全哈希版本而不是明文中的值。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/05/03/a8284ebd4499ef1ab357f8e0f61a91af.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com