如何对给定范围的已哈希值进行哈希处理?

How to hash an already hashed value for a given range?

我正在尝试设计一种一次性密码算法。我想从用户那里获取一个字符串输入并将其重复哈希 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())

但是此代码不起作用。我希望它达到 "re-hash" 的值,并且每次这样做时都会将它添加到数组中。

感谢任何帮助:)

  1. For-loops in Python 可以更容易地实现。只写for i in range(10):,里面什么都不写

  2. hashStore.append(rehash) 使用 rehash 而不是 reHash

  3. 你没有记住你的 reHash 所以你总是尝试散列起始字符串

  4. 如果您想重新散列,您应该将散列转换为字符串: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())

改为使用 for 循环,用初始散列初始化 hashStore,并在每个循环中重新散列最后一个散列散列 (hashStore[-1]):

import hashlib

password= input("Password to hash converter: ")
hashedPassword= hashlib.md5(password.encode())
print("Your hash is: ", hashedPassword.hexdigest())

hashStore= [hashedPassword]
for _ in range(1,100):
    reHash = hashlib.md5(hashStore[-1].hexdigest().encode('utf-8'))
    hashStore.append(reHash)
    print("Rehashed ",reHash.hexdigest())