- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我的代码:
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 的新手,我只是在玩弄它。我认为这会做的是验证用户输入的实际密码的哈希值,但是如果您输入正确的密码,它仍然会出现“访问被拒绝”。谁能指出我正确的方向?
最佳答案
您正在比较两个散列对象,而不仅仅是比较它们的摘要。
将您的 if
更改为 if converted.digest() == real.digest()
应该可以。
通过 if converted == real
你实际上是在比较这两个对象,虽然它们代表一个对同一事物进行哈希处理的哈希对象,但它们是不同的对象,因为 hashlib
哈希对象未实现 __cmp__
、__eq__
或 __ne__
,they fall back to comparing the two objects by identity ,因为它们是两个不同的对象,将返回 false。
来自文档链接:
If no
__cmp__()
,__eq__()
or__ne__()
operation is defined, class instances are compared by object identity (“address”).
通过对它们执行 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']
关于python - Hashlib 哈希没有正确比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695591/
Bellow 是一个代码,它将 hashlib.sha256() 与我的 sha256_test() 函数进行比较,该函数是在哈希率性能方面用原始 Python 编写的。 from time impo
我在google上搜索,发现它是预先安装的,不需要使用pip安装 但是当我通过观看 youtube 运行程序编写但运行时它给了我错误 > Enter md5 hash: b73bf7d3ba1a517
我想做一个列表理解,对两个列表中的元素进行哈希处理。我以为它会是这样的: hashes = [hashlib.md5().update(a + b).digest() for a, b in zip(
当我尝试将 ripemd160 与 hashlib 一起使用时,它说找不到它。 我使用easy_install hashlib安装了hashlib,但仍然找不到ripemd160。 我使用的是 Ubu
我在计算/bin/* 目录下所有文件的所有校验和时遇到问题。我正在用 Python 实现 HIDS,所以我需要计算每个文件的校验和并将其保存在列表中……所以我这里的代码只返回/bin/* 目录的第一个
我想知道在 python 2.7 中观察到的行为的实际原因是什么: import hashlib hashlib.md5('foo') == hashlib.md5('foo') 返回错误。但是……
我正在使用 hashlib 模块来检验关于哈希算法的假设,但我得到了奇怪的结果。我用 Windows fciv 程序检查我的结果。我使用的工作流程是这样的: 从用户那里收集文件和算法。 使用该算法打印
我试图用 sha1 加密一个字符串,但我从服务器收到一个错误: "No Module Named hashlib" 通过使用以下代码: import hashlib encrypted = hashl
这是我的代码: import hashlib real = hashlib.sha512("mom") status = True while status: inp = raw_input(
我正在尝试创建一个具有用户交互的哈希函数。这个想法是,用户选择他/她想要的哈希值(即 md5、sha1 等),然后程序完成其余的工作。 我的代码: hashstring = "hashlib" + f
如果我这样做,我会得到这个: >>> import hashlib >>> hashlib.sha224('Nguyễn').hexdigest() Unsupported characters in
我知道之前有人问过这个问题,我也看到了一些答案,但这个问题更多的是关于我的代码和完成这个任务的最佳方式。 我想扫描一个目录并查看该目录中是否有任何重复项(通过检查 MD5 哈希值)。以下是我的代码:
我正在尝试使用 hashlib 模块中的函数 hashlib.md5() 计算文件的 md5 哈希值。 所以我写了这段代码: Buffer = 128 f = open("c:\\file.tct",
我想创建一个 hashlib 实例,update() 它,然后以某种方式保留它的状态。稍后,我想使用此状态数据重新创建对象,并继续 update() 它。最后,我想获取总累积运行数据的 hexdige
我在 Dreamhost 服务器上运行 Python 脚本。我们的 Python 脚本使用 Python 2.7 - 我们进行了自定义安装,因为 Dreamhost 使用 Python 2.6。在 1
我刚刚从源代码安装了 Python 2.6.6,我得到了什么: >>> import hashlib Traceback (most recent call last): File "", lin
我正在努力完全理解加密的工作原理和编码方式,尤其是使用 Python 时。我只是想了解基础知识并以最简单的形式创建代码。 我将在两个不同的站点之间传递用户 ID,但显然我需要使用私钥对其进行加密,以便
The Python 2.7 documentation关于 hashlib 哈希器有这样的说法: hash.update(arg) Update the hash object with t
我正在尝试编写一个脚本,为指定根目录中的所有文件名和目录名生成哈希值。到目前为止,这是我的脚本: import hashlib import os import sys class Hasher:
我正在开发一个小的 python 程序,该程序本质上将使用 word 文件强制执行 md5 哈希。该程序获取您的哈希值,然后您可以选择一个文件用作单词列表。然后它将在文件中逐行检查并生成一个 md5
我是一名优秀的程序员,十分优秀!