3

I wanted to write a Python script to bruteforce hashes that permits the user to insert the hashing algorithm as a string (Result: algorithm = "md5"), but when I tried to use it like this in the hashlib library: guess_hash = hashlib.algorithm(bytes(guess)).hexdigest(), it obviously gave me this error: AttributeError: module 'hashlib' has no attribute 'algorithm' So I did a quick research and I tried using getattr like this: getattr(hashlib,guess(bytes(guess1))).hexdigest() (probably really wrong) and it gave me this error: TypeError: string argument without an encoding. What should I do? Thanks in advance and sorry for the noobiness :)

3 Answers 3

1

You missed passing the actual algorithm name to the getattr call.

Try this:

getattr(hashlib, 'md5')(bytes(guess)).hexdigest()
Sign up to request clarification or add additional context in comments.

Comments

0

That's actually bytes complaining (which it will with Python 3 but not Python 2). It would appear that you've also swapped the meanings of algorithm and guess in your getattr, and you'll want to do something like

getattr(hashlib, algorithm)(bytes(guess, 'utf-8')).hexdigest()

Comments

0

Simple is better than complex. You can just have a bunch of if statements and do the correct call in those. If there are too many, you could use a hashmap where the key is a string and the value a function.

getattr is, however, the correct call to fetch an attribute with a variable, but the error tells you that you cannot convert a string to a bytestring without specifying the encoding. The bytes function allows you to specify encoding like this:

a_byte_string = bytes(a_regular_string, encoding='utf8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.