1
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)

I am trying to figure out a way to iterate the digests of hashlib. Is a list viable for this? (I know it doesn't work as I'm passing it a string).

Alternatively, what options exist for doing this sort of iteration?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Replace `hashlib.digest[i]` by `getattr(hashlib, i)`. Should work, not the best practice still. – Learning is a mess Dec 07 '21 at 14:42
  • To iterate over functions from a module without knowing their names beforehand: https://stackoverflow.com/questions/21885814/how-to-iterate-through-a-modules-functions – mkrieger1 Dec 07 '21 at 14:48
  • @mkrieger1 thanks handy to know , though I know the digests so wanted to just iterate through them selectively – 01Cyber_cyber10 Dec 07 '21 at 15:08

1 Answers1

2

If you just want to iterate over a few known functions, I suggest that instead of having a list of names and digging out the corresponding functions, you simply have a list of functions:

digest = [ hashlib.sha1, hashlib.sha256, hashlib.md5 ]
...
for d in digest:
    ...
    hasher = d()
    hasher.update(testString + s)
    h2 = hasher.hexdigest()
Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15