2

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.

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

Please dont flame me ;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

  • 1
    What have you tried? Stack Overflow is a site for questions with definitive answers, not for general discussion. You're far more likely to get useful answers if you try to solve your problem yourself and then ask about a specific obstacle you've run into, rather than just presenting your task and asking generically for help. It's also more likely to be acceptable to your instructors if you ask for help with a specific issue, rather than just posting your homework assignment and asking for us to solve it for you! – Blckknght May 03 '14 at 23:02
  • @Blckknght Edited my post to show what i have tried, The code for adding to a dictionary i had found on stackoverflow, I was just dont understand assigning values to keys – Taylor Stevens May 03 '14 at 23:16
  • Possible duplicate of [I need to securely store a username and password in Python, what are my options?](https://stackoverflow.com/questions/7014953/i-need-to-securely-store-a-username-and-password-in-python-what-are-my-options) – Stevoisiak Feb 16 '18 at 15:40

2 Answers2

4

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
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..

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
3

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.

Frank Riccobono
  • 1,043
  • 6
  • 13