python - 如何散列给定范围的已散列值?
问题描述
我正在尝试设计一种一次性密码算法。我想从用户那里得到一个字符串输入,并重复哈希它 100 次,然后将每个存储到一个数组中。我被困在需要反复散列字符串的部分。
我已经尝试了基础知识,我知道如何使用 hashlib 获取字符串值的哈希值。在下面的代码中,我尝试以这种方式应用它 10 次,但我觉得有一种更简单的方法可以实际工作。
import hashlib
hashStore= []
password= input("Password to hash converter: ")
hashedPassword= hashlib.md5(password.encode())
print("Your hash is: ", hashedPassword.hexdigest())
while i in range(1,10):
reHash= hashlib.md5(hashedPassword)
hashStore.append(rehash)
i= i+1
print("Rehashed ",reHash.hexdigest())
但是,此代码不起作用。我希望它能够“重新散列”该值,并且每次都将其添加到数组中。
任何和所有的帮助表示赞赏:)
解决方案
Python 中的 for 循环可以更容易地实现。只需
for i in range(10):
在循环里面不写任何东西。hashStore.append(rehash)
使用rehash
而不是reHash
你不会记住你的
reHash
所以你总是尝试散列起始字符串如果你想重新散列它,你应该将你的散列转换为字符串:
reHash.hexdigest().encode('utf-8')
这是完整的工作代码:
import hashlib
hashStore = []
password = input("Password to hash converter: ")
hashedPassword = hashlib.md5(password.encode())
print("Your hash is: ", hashedPassword.hexdigest())
reHash = hashedPassword
for i in range(10):
reHash = hashlib.md5(reHash.hexdigest().encode('utf-8'))
hashStore.append(reHash)
print("Rehashed ",reHash.hexdigest())
推荐阅读
- c++ - 在着色器中访问 SSBO 时程序崩溃
- css - Ionic 5:向下滑动时删除可折叠标题和离子内容之间的空白
- python - if-not-none-then-none-else-default 的简短声明
- c++ - 复制构造函数打开 allocator.h 时出错
- oracle - 如何处理 PL-SQL 包错误 PLS-00103:在预期以下情况之一时遇到符号“(”::=)
- c - 我怎样才能从这个函数取回我的主函数的迭代值?
- react-native - rn-apple-healthkit 需要更多信息才能开始使用它
- sql - 在sql查询中,是否有声明Join的顺序?
- javascript - 基本/愚蠢的问题。为什么两个图书馆在我看来像是两个不同的世界?(p5.js - two.js)
- javascript - Nodejs-异步函数-TypeError:无法设置未定义的属性“温度”