-2

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!")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
null
  • 147
  • 1
  • 14
  • I suggest you to print the types of the arguments you compare... That will probably give you the answer – Tomerikoo Dec 22 '21 at 23:57
  • `hashed == resulte.digest()` you compare a string with bytes, this will always be `False` – mozway Dec 22 '21 at 23:58
  • not sure, but try to see if you are not getting `\n` when reading from file. – simpleApp Dec 22 '21 at 23:58
  • A few side notes: there is no need for that `if not os.path.exists` part - when opening a file with `'w'` the file will be created if it doesn't exist already. You can replace the whole `if hashed != resulte.digest():` line with a simple `else:`... – Tomerikoo Dec 23 '21 at 00:15
  • Thanks!! Wrapping resulte.digest() around str worked. – null Dec 23 '21 at 12:49

2 Answers2

3

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!")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
1

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!")
Let'sbecool
  • 104
  • 6