I am supposing your csv will be like this:
user1, hash_pass_u1
user2, hash_pass_u2
user3, hash_pass_u3
...
Just one note before the solution. You are importing the CSV module of Python and you did not use it in your code, such a silly import, just use it.
The solution is simple
import csv
file="yourcsv.csv"
found = False
username = input('Write your username: ')
password_csv = None
with open(file, newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for row in reader:
# row[0] is the first element, the username and row[1] the hash of the password
if row[0] == username:
password_csv = row[1]
found = True
break
if not found:
print('The username is not in our DB.')
while True:
passw = input('Let me your password: ')
hash_passw = your_method_to_get_the_hash(passw)
if hash_passw == password_csv:
print('Congrats, you are logged.')
break
else:
print('Wrong password dude, try again.')
In this way you only read the file once and you will use the CSV Module.
I’m supposing the format of your CSV if it is another format is easy to change the implementation of this solution. If you need some help with the CSV Module the documentation is here, for python2 and python3
Explanation of what you are doing wrong.
When you do the following sentence:
csv = open('inlog.csv', 'r').read().split('\n')[1].split(';')
You are opening the file, read all the file then split the file by \n
character, with this you would obtain the following list ['user1;pass1';'user2;pass2','user3;pass3',...]
and the last step you do there, is select the second element with [1]
, the result of this would be the string 'user2;pass2'
. But the statement does not finish here, there is another split that would give you the list ['user2','pass2']
.
So you are comparing that the username is admin or is in the list ['user2','pass2']
. The same happens when you try to compare the password, but this time you select the third element.
1
solved Find password by username