I am creating a simple database style program for a project in school and I am using a simple hash and salt algorithm for storing passwords. I would like to use the Unit Separator and Record Separator ASCII codes to separate attributes and records in the database, but I can't find anything on them in Python. Here is my code so far:
import os
import hashlib
import sys
users = {}
def new_user():
while True:
username = str(input('Please Enter Your Desired Username > '))
if username in users.keys():
print('Username already exists, please try again')
else:
break
while True:
password = str(input('Please Enter a Password Longer than 5 Characters > '))
if len(password) < 6:
print('Too Short, Try Again')
else:
break
password = str.encode(password)
salt = os.urandom(256)
hashed = hashlib.sha256(password+salt).hexdigest()
new = {'username' : username,
'password' : hashed,
'salt' : salt }
users[username] = new
towrite = new['username']+'\x1f'+new['password']+'\x1f'+str(new['salt'])+'\x1e'
with open('users.txt', 'a') as userfile:
userfile.write(towrite)
print('New User Created: Welcome %s' % username)
return()
def login():
while True:
with open('users.txt', 'r') as userfile:
users = userfile.read().split('\x1e')
for user in users:
user = user.split('\x1f')
username = str(input('Please Enter Your Username > '))
password = str(input('Please Enter Your Password > '))
salt = user[2]
password = hashlib.sha256(password + salt).hexdigest()
for user in users:
if (username == user[0]) and (password == user[1]):
print('You\'re In!')
return()
print('Invalid Username or Password')
while True:
choice = str(input('Continue Adding? > '))
if choice == 'n':
break
else:
new_user()
while True:
choice = str(input('Continue Logging? > '))
if choice == 'n':
break
else:
login()
The problem here is that when joining the attributes in the new_user() function, the string that is written to the file does not contain '\x1e' or '\x1f'. How can I fix this or otherwise implement these control codes?