Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a variable as an attribute in python

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

like image 836
Desertcod98 Avatar asked Apr 28 '26 18:04

Desertcod98


1 Answers

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

Try this:

getattr(hashlib, 'md5')(bytes(guess)).hexdigest()
like image 60
Felipe Ferri Avatar answered Apr 30 '26 06:04

Felipe Ferri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!