Heres my code:
import hashlib
real = hashlib.sha512("mom")
status = True
while status:
inp = raw_input("What's the password?")
converted = hashlib.sha512(inp)
if converted == real:
print "Access granted!"
status = False
else:
print "Access denied."
I’m new to hashlib, and I’m just playing around with it. What I thought this would do is validate the users input to the hash of the actual password, however if you enter the correct password, it still comes up “Access denied.” Can anyone point me in the right direction?
You’re comparing two hash objects instead of just comparing their digests.
Change your
iftoif converted.digest() == real.digest()and that should work.By doing
if converted == realyou’re actually comparing the two objects, which while they represent a hash object that does hash to the same thing, they are different objects and sincehashlibhash objects don’t implement__cmp__,__eq__, or__ne__, they fall back to comparing the two objects by identity, which since they are two distinct objects, will return false.From the doc link:
You can see that those objects don’t implement those operators by doing a
dir()on them: