Hi there developer folks,
I'm trying to write a little proof-of-concept program on repl.it which stores credentials securely using BCrypt. To save myself faffing with SQL, I'm prototyping using the built in repl.it database to store credentials.
The method in question is as follows:
def SignUp(self):
'''Takes the username and password used to initialise the object, and stores them securely'''
username = self.__username_input
# Convert password input into bytes object
password_bytes = bytes(self.__password_input, "UTF-8")
# Hash password using the BCrypt algorithm
hashed_password = bcrypt.hashpw(password_bytes, bcrypt.gensalt(rounds=14))
username_available = True # True for testing - change to False for production
# Checks if the username is available
try:
db[username]
except KeyError:
username_available = True
if username_available:
# Store password in database
db[username] = hashed_password
self.Username = username
self.IsAuthenticated = True
print(db[username])
return True
else:
return False
Currently, when I run this, I get the following error:
TypeError: Object of type bytes is not JSON serializable
Now I've tried changnig db[username] = hashed_password
to db[username] = str(hashed_password)
, which works fine, but then when I grab the hashed password back out of the database like so bcrypt.checkpw(password_bytes, bytes(db[username], "UTF-8"))
, BCrypt throws this error:
ValueError: Invalid salt
Any suggestions would be awesome.
Thanks!