Let me walk you through what this larger snippet of code would do, maybe that will help you understand it.
# Import getpass to securely get use's input. Remember we used this for that rock paper
# scissors game?
from getpass import getpass
# Import random to create the salt
from random import randint
# Import the database to create the account
from replit import db
# CREATING AN ACCOUNT
# Get the username. You'd probably want to check if the account already exists, but this
# is just an example
username = input("Username: ")
# Get the password. You can't see what you type, which is why getpass is so useful.
password = getpass()
# Generate a random salt
salt = randint(1000, 9999)
# Hash the password and salt
password = hash(f"{password}{salt}")
# Create the user in the db. You'll understand why we need to store the salt in a second
db[username] = {"password": password, "salt": salt}
# LOGGING IN
# Code is quite similar to creating an account, but is a bit different.
# Get the username
username = input("Username: ")
# Get the password.
password = getpass()
# Now to just check if the user inputted the correct password, but, wait, we don't know what salt
# the user got! How do we figure out what to hash? Oh, yeah, thankfully, we thought ahead and
# already stored the salt.
salt = db[username]["salt"]
# Now we can hash it and check if they entered the right password.
password = hash(f"{password}{salt}")
if password == db["username"]["password"]:
print("Yay! You entered the right password")
else:
print("Incorrect password.")
Hey @MichaelDaburn, thatâs because youâre attempting to retrieve the salt value, but itâs not functioning properly. The issue arises because either db[username] or db[username]["salt"] is not structured to support indexing. Could you please share your data structure or the method by which you save the username and its corresponding salt?
Iâve created a test repl and it works! For some reason in the tutorial it didnât want to play ball even though I copied the exact code across in both!
I see. Yeah, because I couldnât replicate it even with the given username and password you used. Iâm guessing it might be an error in the tutorial.
Can someone help me understand why this code doesnât work. I know it has to do with the login() subroutine, but Iâve been fiddling with it for too long and still stuck. The error produced isnât legible to me. Thanks in advance.
from replit import db
import random
def adduser():
username = input("Username: ")
password = input("Password: ")
salt = random.randint(1,9999)
newpassword = f"{password}{salt}"
newpassword = hash(newpassword)
db[username] = {"password": newpassword, "salt": salt}
def login():
username = input("Username: ")
pw = input("Password: ")
salt = db[username]["salt"]
newpw = f"{pw}{salt}"
keys = db.keys()
for key in keys:
if key in keys:
if newpw == db["password"]:
print("Login Successful!")
else:
print("Password incorrect")
else:
print("Username incorrect")
while True:
menu = input("1. Add user\n2. Login\n> ")
if menu == "1":
adduser()
else:
login()
break
Well, first of all, youâre using a hash method that doesnât exist. Instead of doing password.hash(), you should use password = hash(password).
Second of all, you are setting variables to values that donât exist in the database. That is what is causing the error.
Instead, you should set the database values to the already defined variables.
So instead of this:
salt = db[username]["salt"]
...
password = db[username]["password"]