Problem with argument substitution in function

Asked

Viewed 66 times

1

I’m having a problem with the arguments I pass in the function below, I’m trying to pass the argument sha512 for hash_type, so that the code within the function is replaced, and so hash_target would be passed by the correct hash algorithm, but whenever I run the code, python returns an error message saying that the library hashlib has no function hash_type, which shows that the substitution is not happening, which can be done to fix this problem ?

Code:

import sys
import hashlib


def main(hash_type,wordlist='palavrasptbr'):
    hash_target = input("Hash :\n")
    hash_target = bytes(hash_target.encode('utf-8'))
    wordlist += '.txt'
    archive = open(wordlist, 'r')
    print(archive)

    hash_target = bytes(hash_target)

    print(hashing_sha512(bytes(hash_target)))

def hashing_sha512(hash_target):
    hashing = hashlib.sha512()
    hashing.update(hash_target)
    return hashing.hexdigest()


if __name__ == "__main__":
    try:
        main(sys.argv[1], sys.argv[2])
    except KeyboardInterrupt:
        pass
  • 2

    put the full code bless

1 answer

0

First: your code does not work without modifications. It is not a good way to call for help.

  1. In his function main the argument hash_type is not used.
  2. You upload the file to archive = open(wordlist, 'r') but does not use it.

Try a different approach:

  1. Make your argv[1] a string that gives you a function:

    if argv[1] == 'sha512':
        hashfunc = hashlib.sha512
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.