'How can I check with python if a hash is stored in a txt file
I was trying to write a simple log-in-programm, that stores the usernames and password as hashes in a .txt file (I know that that's not safe but I'm just trying to learn). I also tried to implement a system that checks, if a username is already taken. And that's where my problem is. ("#check if it's already taken" in the code)
When the username is already taken, it tells me to choose a new one. But when I'm inputting the same name again, after it told me to take a new one, it just continious with the code, even though the name exists already in the file. So is there a method to get a loop that checks over and over again, if the username, I'm trying to input, is already in the file?
import hashlib, time, os
def register():
#create new username
user = input('Create your username: ')
hashuser = hashlib.sha256(user.encode())
hexhashuser = hashuser.hexdigest()
#check if its already taken
with open('py\Hashed Login\Sign in data.txt') as temporary:
while hexhashuser in temporary.read():
user = input('Sorry, that username is already taken. Please chose a new one: ')
hashuser = hashlib.sha256(user.encode())
hexhashuser = hashuser.hexdigest()
data = open('py\Hashed Login\Sign in data.txt', 'a' )
data.write(hexhashuser)
if hexhashuser not in temporary.read():
data = open('py\Hashed Login\Sign in data.txt', 'a' )
data.write(hexhashuser)
#write to file
data.write('+')
time.sleep(1.5)
#create new password
password = input('Please choose your new password: ')
check = input('Please enter your password again: ')
while check != password:
check = input('Wrong password. Please enter it again: ')
hashpassword = hashlib.sha256(password.encode())
hexhashpassword = hashpassword.hexdigest()
data.write(hexhashpassword)
data.write('''
''')
data.close
def login():
print('placeholder')
register()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
