Hashlib 哈希值无法正确比较

发布于 2024-11-24 05:17:24 字数 410 浏览 1 评论 0原文

这是我的代码:

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."

我是 hashlib 的新手,我只是在玩它。我认为这会验证用户输入的实际密码的哈希值,但是如果您输入正确的密码,它仍然会出现“访问被拒绝”。有人能指出我正确的方向吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-12-01 05:17:26

如果你比较摘要,那应该有效:

if converted.digest() == real.digest():
   ...

If you compare the digests, that should work:

if converted.digest() == real.digest():
   ...
二智少女 2024-12-01 05:17:26

您正在创建两个不同的 hashlib 对象,并且它们不相等。您需要的是比较摘要:

if converted.digest() == real.digest():

You are creating two different hashlib objects and they are not equal. What you need is to compare the digest:

if converted.digest() == real.digest():
荒人说梦 2024-12-01 05:17:25

您正在比较两个哈希对象,而不仅仅是比较它们的摘要。

将您的 if 更改为 if conversion.digest() == real.digest() ,这应该可以工作。

通过执行 if conversion == real 你实际上是在比较两个对象,虽然它们代表一个散列到同一事物的散列对象,但它们是不同的对象,并且由于 hashlib code> 哈希对象不实现 __cmp____eq____ne__他们会根据身份比较两个对象,因为它们是两个不同的对象,所以将返回 false 。

从文档链接:

如果没有定义 __cmp__()__eq__()__ne__() 操作,则通过对象标识来比较类实例(“地址”)。

您可以看到这些对象没有通过对它们执行 dir() 来实现这些运算符:

>>> test = hashlib.sha512('test')
>>> dir(test)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__',
 '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', 'block_size', 'copy', 'digest',
 'digest_size', 'digestsize', 'hexdigest', 'name', 'update']

You're comparing two hash objects instead of just comparing their digests.

Change your if to if converted.digest() == real.digest() and that should work.

By doing if converted == real you'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 since hashlib hash 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:

If no __cmp__(), __eq__() or __ne__() operation is defined, class instances are compared by object identity (“address”).

You can see that those objects don't implement those operators by doing a dir() on them:

>>> test = hashlib.sha512('test')
>>> dir(test)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__',
 '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', 'block_size', 'copy', 'digest',
 'digest_size', 'digestsize', 'hexdigest', 'name', 'update']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文