【问题标题】:How to iterate over functions from a library?如何迭代库中的函数?
【发布时间】:2022-01-12 15:25:59
【问题描述】:
import hashlib

digest = ["sha1", "sha256", "md5"]

s = "5UA@/Mw^%He]SBaU".encode('utf-8')
h = "3281e6de7fa3c6fd6d6c8098347aeb06bd35b0f74b96f173c7b2d28135e14d45"

dictionary = open("dictionary.txt", "r")
for i in digest:
    for line in dictionary:
        testString = bytes(line, 'utf-8')
        h2 = hashlib.digest[i](testString + s).hexdigest()
        print(h2)
        if h in h2:
            print("the password is:", line)

我正在尝试找出一种方法来迭代 hashlib 的摘要。清单是否可行? (我知道它不起作用,因为我正在传递一个字符串)。

或者,进行这种迭代有哪些选择?

【问题讨论】:

  • hashlib.digest[i] 替换为getattr(hashlib, i)。应该有效,但仍然不是最佳做法。
  • 在事先不知道函数名称的情况下从模块中迭代函数:stackoverflow.com/questions/21885814/…
  • @mkrieger1 感谢方便知道,虽然我知道摘要所以只想有选择地遍历它们

标签: python python-3.x loops iteration


【解决方案1】:

如果你只是想迭代几个已知的函数,我建议不要有一个名称列表并挖掘出相应的函数,你只需有一个函数列表:

digest = [ hashlib.sha1, hashlib.sha256, hashlib.md5 ]
...
for d in digest:
    ...
    hasher = d()
    hasher.update(testString + s)
    h2 = hasher.hexdigest()

【讨论】:

  • 这真的很有帮助,谢谢。
猜你喜欢
  • 2017-05-28
  • 1970-01-01
  • 2011-02-24
  • 2020-07-04
  • 2021-10-28
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多