loekarbona
loekarbona

Reputation: 21

Python won't accept two same strings as the same

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.

Upvotes: 1

Views: 75

Answers (3)

Mohd
Mohd

Reputation: 5613

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]

Upvotes: 0

Zach Gates
Zach Gates

Reputation: 4130

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()

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249153

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

if userhash.strip() == hexdigest

Upvotes: 0

Related Questions