Jump to content

Recommended Posts

Posted

Im trying to figure out how to import user names and passwords into a list in python from a text file. Something like this:

List = ""

list = [open(*.txt)]

can i get some direction?

Posted

Does the text file already contain all the usernames and passwords or just the passwords.

Posted

My suggestion would probably be to create two separate arrays, one for storing the "Usernames[]" and the passwords "Passwords[]" values as it reads the text files.

And then have two "for loops" for each corresponding array for writing the credentials back to file.

Don't know If that makes sense to you.

Posted


username = " "
password = " "
usernameList = []
passwordList = []
login = "n"
indexer = 0

while (login == "n"):
    print "Please enter your Username"
    username = str.lower(raw_input("> "))
    str.lower(username)
    indexer = usernameList.index(username)
    print "Please enter your Password"
    password = raw_input("> ")

    if (username == usernameList[indexer] and password == passwordList[indexer]):
        print "Login Successful!"
        login = "y"
    else:
        print "Login Failed, Please try again"

Here is what i have thus far. I just need to import the lists into the arrays from text files

Posted

Here we go, just a little something that does the trick. It reads from a file called "passwd" in the current directory. The formatting of the logins is "username:password" it will parse those and then put them into a dictionary. This has benefits because it will automatically expand to however many logins there are in the file. I also took out all of the lowering of strings, it's unnecessary and a security risk.

I've also added some basic SHA encryption as well. An already SHA encrypted password is placed in the login file instead of a cleartext password. Then the program computes the SHA hash of the user given password. If the two of them match then the user is logged in.

There might be a few bugs you run into, for example if the "username:password" string isn't formatted correctly in the password file you may have some problems.

Overall it's fairly easy to follow but if you've got any questions just ask.

Here's my passwd file there are 2 users, a normal user and a guest user, the user Alias has a private password (:P) and the guest user has a password of "password"

Alias:2dd00738ed848e5fad7070ef9104c62c2dfe82ab51e3ef6e1bb81bd4caecad63
guest:5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

import string as str
import hashlib as crypto

username = ""
password = ""
login = "n"
logins = dict()

filePointer = open("passwd", "r")
for line in filePointer:
	line = str.strip(line, "\n")
	loginInfo = str.split(line, ":", 2)
	logins[loginInfo[0]] = loginInfo[1]

while (login == "n"):
	print "Please enter your Username"
	username = raw_input("> ")
	print "Please enter your Password"
	password = crypto.sha256(raw_input("> ")).hexdigest()

	if(logins[username] == password):
		login = "y"
		print "Login Successful!"
	else:
		print "Login Failed\n"

Posted

As Alias did, if you are trying to set up an authentication system with python, you should use hashlib so that if someone finds the file with the usernames and passwords, they can't just have instant access to your system. Here is a topic explaining it more in depth.

intro to cryptography

Posted

As Alias did, if you are trying to set up an authentication system with python, you should use hashlib so that if someone finds the file with the usernames and passwords, they can't just have instant access to your system. Here is a topic explaining it more in depth.

intro to cryptography

That's actually a really good link although it doesn't explain the cryptography to the extent that it should. Still a good read though.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...