How to use sha256 hash in Python

I am trying to read in a password file. Then I try to compute a hash for each password and compare it to the hash, which I already have to determine if I have discovered the password. However, the error message I keep getting is "TypeError: Unicode objects must be encoded before hashing". Here is my code:

from hashlib import sha256

with open('words','r') as f:
    for line in f:

        hashedWord = sha256(line.rstrip()).hexdigest()

        if hashedWord == 'ca52258a43795ab5c89513f9984b8f3d3d0aa61fb7792ecefe8d90010ee39f2':
            print(line + "is one of the words!")

      

Can anyone help and provide an explanation?

+3


source to share


1 answer


The error message means what exactly it says: you have a Unicode string. You cannot SHA-256-hash a Unicode string, you can only use hash bytes.

But why do you have a Unicode string? Since you are opening the file in text mode, this means that you are implicitly asking Python to decode the bytes in that file (using the default encoding) to Unicode. If you want to get raw bytes, you must use binary mode.

In other words, just change this line:

with open('words','r') as f:

      

... to:



with open('words', 'rb') as f:

      


You may notice that after you fix the line, print

an exception is thrown. What for? because you are trying to add bytes

to str

. You also run out of space and you are printing a line with no separator lines. You can fix all of this by using two arguments for print

(as in print(line.rstrip(), "is one of the words")

).

But then you get the output, like b'\xc3\x85rhus' is one of the words

when you want to print Århus is one of the words

. This is because you now have bytes, not strings. Since Python no longer decrypts you, you will need to do it manually. To use the same default encoding that sometimes works when you don't specify the encoding to open

, just call decode

with no argument. So:

print(line.rstrip().decode(), "is one of the words")

      

+5


source







All Articles