So i recently wrote this script to make a login interface that then stores all the login details into an encrypted .data file in python but for some reason it always returns me an error. I don't know how to fix it. Can someone help me?
import os
import hashlib
def encrypt_password(password):
encrypted_password = hashlib.sha256(password.encode()).hexdigest()
return encrypted_password
def create_account(username, password):
encrypted_password = encrypt_password(password)
data_file = open(username '.data', 'w')
data_file.write(username '\n')
data_file.write(encrypted_password)
data.file.close()
print('Account successfully created!')
def login(username, password):
encrypted_password = encrypt_password(password)
if os.path.exists(username '.data'):
data_file = open(username '.data', 'r')
stored_username = data_file.readline().rstrip('\n')
stored_password = data_file.readline()
if username == stored_username and encrypted_password == stored_password:
print('Login successful!')
else:
print('Incorrect username or password')
else:
print('Account does not exist. Please create an account first.')
user_input = input('Do you want to create (c) or login (l) to an account? ')
if user_input == 'c':
username = input('Enter username: ')
password = input('Enter password: ')
create_account(username, password)
elif user_input == 'l':
username = input('Enter username: ')
password = input('Enter password: ')
login(username, password)
else:
print('Invalid input')
it keeps returning me this error
CodePudding user response:
Good day,
I saw the mistake. It is a very minor mistake and can be easily fixed. All you have to do is put your if..else.. statement at the top and move some of the code to their functions. Like this:
import os
import hashlib
def encrypt_password(password):
encrypted_password = hashlib.sha256(password.encode()).hexdigest()
return encrypted_password
def create_account(username, password):
encrypted_password = encrypt_password(password)
data_file = open(username '.data', 'w')
data_file.write(username '\n')
data_file.write(encrypted_password)
data_file.close()
print('Account successfully created!')
def login(username, password):
global encrypted_password
encrypted_password = encrypt_password(password)
if os.path.exists(username '.data'):
data_file = open(username '.data', 'r')
stored_username = data_file.readline().rstrip('\n')
stored_password = data_file.readline()
if username == stored_username and encrypted_password == stored_password:
print('Login successful!')
else:
print('Incorrect username or password')
else:
print('Account does not exist. Please create an account first.')
user_input = input('Do you want to create (c) or login (l) to an account? ')
if user_input == 'c':
username = input('Enter username: ')
password = input('Enter password: ')
create_account(username, password)
elif user_input == 'l':
username = input('Enter username: ')
password = input('Enter password: ')
login(username, password)
else:
print('Invalid input')
I hope that this solves the problem :)