Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对给定范围内已散列的值进行散列?_Python_Python 3.x_Cryptography_Md5_Hashlib - Fatal编程技术网

Python 如何对给定范围内已散列的值进行散列?

Python 如何对给定范围内已散列的值进行散列?,python,python-3.x,cryptography,md5,hashlib,Python,Python 3.x,Cryptography,Md5,Hashlib,我正在尝试设计一个一次性密码算法。我想从用户那里获取一个字符串输入,并将其反复散列100次,然后将每个字符串存储到一个数组中。我被困在需要反复散列字符串的部分 我已经尝试了一些基础知识,我知道如何使用hashlib获得字符串值的哈希值。在下面的代码中,我用这种方法尝试了10次,但我觉得有一种更简单的方法可以实际工作 import hashlib hashStore= [] password= input("Password to hash converter: ") hashedPasswo

我正在尝试设计一个一次性密码算法。我想从用户那里获取一个字符串输入,并将其反复散列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循环可以更容易地实现。只需为范围(10)内的i编写
    ,循环中没有任何内容

  • hashStore.append(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())
    

    使用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())
    

    rehash
    rehash
    是不同的变量。注意你的编码标准。您也从不为循环加素数(即
    i=0
    )。这个代码应该在几个地方出错了。您可能只想使用
    for
    循环,而不是启动
    ,而
    循环:
    for在范围(1,10)内:
    是的,这很有效,谢谢!我不知道转换部分,今天学到了一些新东西。
    对于范围(1,10)中的我:
    将只循环9次。谢谢,修复了它。