The following is my code. I have been trying to fix the code to perform dictionary attack on a hash (SHA-1) and I am getting the following result. P.S. I am a beginner to coding.
import hashlib
import random
#plug in the hash that needs to be cracked
hash_to_crack = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
#direct the location of the dictionary file
dict_file = "C:/Users/kiran/AppData/Local/Programs/Python/Python37/dictionary.txt"
def main():
with open(dict_file) as fileobj:
for line in fileobj:
line = line.strip()
if hashlib.sha1(line.encode()).hexdigest() == hash_to_crack:
print ("The password is %s") % (line);
return ""
print ("Failed to crack the hash!")
return ""
if __name__ == "__main__":
main()
The result:
RESTART: C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py
The password is %s
Traceback (most recent call last):
File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 20, in <module>
main()
File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 13, in main
print ("The password is %s") % (line);
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
This line is not valid python3 syntax: (EDIT: it's a valid syntax ! But not what you want :) )
Use this instead:
Also, accordig to the error message, line might be None.