How do I verify a password with it's hashed value to allow login validation? I am sending queries to mysqlite database
I am trying to match the user password with its already hashed value stored in my SQLite database. The hash value retrieved by the SQL query and stored in the fetched_password variable is the same, yet I cannot verify the login. I have shared the function that I have coded this in. Feel free to request any more information. I am new to programming and have been stuck with this issue for some time.
Here is my Code:
def login():
print("Please log in to access your dashboard.\n")
username_entry = input("Enter your username:\n")
password_entry = pwinput.pwinput(prompt='Enter your password:\n', mask='*').strip().encode('utf-8')
cursor = connection.cursor()
cursor.execute("""SELECT password FROM user WHERE username = ?""", (username_entry,))
result = cursor.fetchall()
print(result)
fetched_password = result[0][0]
check = bcrypt.checkpw(password_entry, fetched_password)
if check == True:
print(f"Username and Password Matched. Log In Approved. Welcome {username_entry}\nLoading...")
time.sleep(2)
general_dashboard()
else:
print(f"You have entered an incorrect username or password.")
Hashing Function code:
def password_hashing(password:str, secret_answer:str):
"""This function hashes the password and secret开发者_Go百科 answer created by the user and returns this"""
password = password.encode('utf-8')
"""Hash and return user_password"""
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password,salt)
return hashed_password
精彩评论