0

I'm trying to do a simple login system with python that will check the hash (sha256) of a password a then let the user in, but is not working. I've tried many different forms of doing so, but this is the best one I could come up.

import hashlib

username = input("Enter username: ")
password = input("Enter password: ")

#hashing password
password = hashlib.sha256(password.encode())
t_hash = password.hexdigest()
print(t_hash)
if username == "kent" and password == "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3":
    print("Clark Kent logged!")
else:
    print("Wrong credentials")

As expected it is not working and I don't know what I'm missing. Can anybody help me? enter image description here

jps
  • 20,041
  • 15
  • 75
  • 79

1 Answers1

2

you should have match t_hash not password in if clause

if username == "kent" and t_hash == "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3":
      print("Clark Kent logged!")
else:
      print("Wrong credentials")

Don't use sha256 for hashing password use other better PBKDF2,bcrypt etc matching password with == not good idea.

https://qvault.io/cryptography/hashing-passwords-python-cryptography-examples/

Salt and hash a password in Python

gaurav
  • 1,281
  • 1
  • 13
  • 25