r/pythontips • • Apr 19 '21

Python3_Specific Username and password program

I am making a program where I have one file that has usernames, a comma and then passwords. I am making a program where it will output all of them to another file and then add to the end of the line the strength for each one

I know how to open files but could I use a for loop for reading each line and writing it to the other file and adding text to the right of it?

Edit: note: security will not be a requirement for my project so I am fine with working with plain text. I am not using this for a major production situation

31 Upvotes

8 comments sorted by

2

u/ProfCmdrC63 Apr 19 '21

I am making this program just for an experiment and security will not matter in this case. I am looking to also separate usernames and passwords and use dictionaries or triples or lists/arrays if or as needed.

I plan on feeding the password to a password strength calculator (I already made) and then outputting the results to the end of each line in the new file

2

u/spez_edits_thedonald Apr 20 '21 edited Apr 20 '21

you should hash the passwords and store hashes, always. It is not hard.

>>> password = 'neverplaintext'
>>>
>>> hashed = hash(password)
>>>
>>> hashed
-2405056841184542328
>>>
>>> guess = 'password1'
>>> hash(guess) == hashed
False
>>> guess = 'admin12345'
>>> hash(guess) == hashed
False
>>> guess = 'neverplaintext'
>>> hash(guess) == hashed
True

I might use md5 from hashlib:

>>> import hashlib
>>>
>>> def md5(s):
...     md5_str = hashlib.md5(s.encode()).hexdigest()
...     return(md5_str)
...
>>>
>>> password = 'neverplaintext'
>>>
>>> hashed = md5(password)
>>> hashed
'3975055ef84e31d84f4c51fe6f7cec04'
>>>
>>> md5('GUESS ONE') == hashed
False
>>> md5('still wrong') == hashed
False
>>> md5('neverplaintext') == hashed
True

1

u/ProfCmdrC63 Apr 20 '21

I figured out code regarding interacting with files and now I just need to know how to separate values when reading from a file.

6

u/earthboundkid Apr 19 '21
  1. Dealing with passwords is tricky business because you don't want an attacker to be able to get a hold of one file on a laptop or server and then get the keys to the kingdom. See https://auth0.com/blog/hashing-passwords-one-way-road-to-security/ which has some sample Python. This may be moot for the case of just a personal function to move your own passwords around, but if you have multiple users, you need hashing.

  2. See https://docs.python.org/3/library/functions.html#open you can append to a file by putting it into mode 'a' or use https://docs.python.org/3/library/io.html#io.IOBase.seek to move around within a file. The tricky bit is it sounds like you want to do insertions into an already existing file. That's not really possible: all the existing data would need to be shifted down on the disk as you write new stuff. A good alternative is to stream one line of the input file at a time, write out one line of the output file, and then once the output is all written down, use https://docs.python.org/3/library/os.html#os.replace to move the finished new file into the place of the old file.

1

u/ohaz Apr 19 '21

Yes. Open both files (you can either do two nested with statements or just write the normal open statements. Then do a for loop, in the for loop call text = file1.readline() on one file and file2.writeline(text + ' your additional stuff') on the other file

2

u/MH2019 Apr 19 '21

Not a full answer for u OP but a tip (as the above comment is alluding to): it's definitely good to learn "with" as a habit for dealing with file i/o.

See: https://www.google.com/amp/s/cmdlinetips.com/2016/01/opening-a-file-in-python-using-with-statement/

You also don't even need to nest "with" statements, there can be multiple on the same line.

Cheers!

1

u/CraigAT Apr 19 '21

Check out the RealPython site for a good tutorial on opening, reading and writing files. There is also a section about working with two files.