我是Python的新手,因此处于以下情况:

我想对密码进行哈希处理并将其与masterhash进行比较。不幸的是,Python不接受它们:

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"


我已经检查了objectypes:两个字符串

哈希,两次:


  c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec


我真的不明白这个问题。

最佳答案

问题是这一行:

lines = hlist.readlines()


此列表中的每个值都会有一个尾随换行符(print时您可能不会注意到)。确保您strip将其关闭。

userhash = lines[0].strip()

关于python - Python不会接受两个相同的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46077568/

10-15 22:58