Home > Blockchain >  Comparing 2 hash values returning False even though they are the same
Comparing 2 hash values returning False even though they are the same

Time:12-23

How the program works is that the user sends 2 inputs over (userinput and passwordinput). These are compared against the filename of a textfile (which is the username) and the context (which is the hashed password). The passwordinput gets hashed and returns the same as the hashed original password with no diferences, however when I compare with the if statement it returns False. Here's my code:

import os
import hashlib

username = "coolcat"
password = "my secret password!!"

result = hashlib.sha1(password.encode("utf-8"))

if not os.path.exists(f"{username}.txt"):
    open(f"{username}.txt", 'w').close()
with open(f"{username}.txt", "w") as file:
   file.write(str(result.digest()))

userinput = input("Your username please : ")
passwordinput = input("Your password please : ")
resulte = hashlib.sha1(passwordinput.encode("utf-8"))
print(str(resulte.digest()))
try :
    with open(f"{userinput}.txt", "r") as file:
        hashed = file.read()
    print(hashed)

    if hashed == resulte.digest():
        print("Access Granted")
    if hashed != resulte.digest():
        print("Wrong password!!")
except FileNotFoundError :
    print("Username not found!!1!")

CodePudding user response:

You have fooled yourself here by using str to print things. The issue is that hash is a Unicode string and resulte.digest() is a bytes string. They both PRINT the same because you used the str function to convert them. If you have the bytes string b'abc', which contains three bytes, and you call str(b'abc'), you will get back a Unicode string that SAYS b'abc', but it contains 6 characters: the b and the quotes are in there. That's the difference. The file you write is a Unicode file that contains a Unicode string, and that string starts with the literal characters "b'".

The solution is to open your file in binary mode:

import os
import hashlib

username = "coolcat"
password = "my secret password!!"

result = hashlib.sha1(password.encode("utf-8"))

with open(f"{username}.txt", "wb") as file:
   file.write(result.digest())

userinput = input("Your username please : ")
passwordinput = input("Your password please : ")
resulte = hashlib.sha1(passwordinput.encode("utf-8"))
print(resulte.digest())
try :
    with open(f"{userinput}.txt", "rb") as file:
        hashed = file.read()

    if hashed == resulte.digest():
        print("Access Granted")
    if hashed != resulte.digest():
        print("Wrong password!!")
except FileNotFoundError :
    print("Username not found!!1!")

CodePudding user response:

Assuming you want to compare two strings, change hashed output to a string too. other than that you are comparing bytes with string which will always return false.

import os
import hashlib

username = "coolcat"
password = "my secret password!!"

result = hashlib.sha1(password.encode("utf-8"))

if not os.path.exists(f"{username}.txt"):
    open(f"{username}.txt", 'w').close()
with open(f"{username}.txt", "w") as file:
   file.write(str(result.digest()))

userinput = input("Your username please : ")
passwordinput = input("Your password please : ")
resulte = hashlib.sha1(passwordinput.encode("utf-8"))
print(str(resulte.digest()))
try :
    with open(f"{userinput}.txt", "r") as file:
        hashed = str(file.read())
    print(str(hashed))

    if hashed == str(resulte.digest()):
        print("Access Granted")
    if hashed != str(resulte.digest()):
        print("Wrong password!!")
except FileNotFoundError :
    print("Username not found!!1!")
  • Related