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.")