0

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'
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    Please, don't use capslock for writing question. Also, take some time to explain what you are trying to do. To learn how to ask better questions, please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic). This will help others to help you. If you have questions, provide your code as [mvce](https://stackoverflow.com/help/mcve). – TwistedSim Apr 17 '18 at 23:53
  • You can use [hashcat](https://github.com/hashcat/hashcat) instead of trying to program this yourself. – Nick Apr 17 '18 at 23:54

2 Answers2

2

You're using Python 3, where print is a function. The line:

print ("The password is %s") % (line)

calls the function print with the argument "The password is %s". The function returns None. Then None % (line) gives the error message you see.

Most idiomatic is to write the line this way instead:

print("The password is", line)

Other ways that would work:

print("The password is %s" % line)
print(("The password is %s") % (line))
Tim Peters
  • 67,464
  • 13
  • 126
  • 132
-1

This line is not valid python3 syntax: (EDIT: it's a valid syntax ! But not what you want :) )

print ("The password is %s") % (line);

Use this instead:

print ("The password is %s" % line)

Also, accordig to the error message, line might be None.

TwistedSim
  • 1,960
  • 9
  • 23
  • 2
    See my answer: the syntax is fine in Python 3 - it just doesn't at all do what they hoped it would do ;-) If the syntax were wrong, they would gotten a `SyntaxError ` instead. The problem is that that `print()` function returns `None`, and _then_ `None % a_string` makes no sense (which _is_ the error message they got). – Tim Peters Apr 18 '18 at 00:01