將用戶名和密碼存儲到字典中?

[英]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