1

I'm quite a newcomer to Python and I am stuck in the following situation:

I want to hash a password and compare it with the masterhash. Unfortunately Python doesn't accept them as the same:

import hashlib
h=hashlib.sha512()
username='admin'
username=username.encode('utf-8')
h.update(username)
hexdigest=h.hexdigest()
hlist=open("database.txt")#masterhash
lines=hlist.readlines()
userhash=lines[0]#masterhash in line 0
if userhash == hexdigest: # it doesent accept them as the same
        text = "True"
else:
        text="False"

I already checked the objectypes: both string

The hash, both times:

c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec

I really don't understand the problem.

1
  • yes c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec Commented Sep 6, 2017 at 14:19

3 Answers 3

1

The problem is this line:

lines = hlist.readlines()

Each value in this list will have a trailing newline (which you may not notice when printing). Make sure you strip that off.

userhash = lines[0].strip()
Sign up to request clarification or add additional context in comments.

Comments

0

readlines() returns lines with newlines at their ends. You are comparing "A" with "A\n". Try this:

if userhash.strip() == hexdigest

Comments

0

When you use readlines() you get a list of the lines with the new line character at the end of each line, you can do one of two options:

Option #1:

lines = hlist.readlines()
userhash = lines[0].rstrip()

Option #2:

lines = hlist.read().splitlines()
userhash = lines[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.