Skip to main content
edited title
Link
Robert Harvey
  • 181.5k
  • 48
  • 349
  • 516

How to use sha256 hash in Python: TypeError: Unicode-objects must be encoded before hashing

I am trying to read in a file of passwords. Then I am trying to compute the hash for each password and compare it to a hash 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: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 please help and provide an explanation?

I am trying to read in file of passwords. Then I am trying to compute the hash for each password and compare it to a hash 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 please help and provide an explanation?

I am trying to read in a file of passwords. Then I am trying to compute the hash for each password and compare it to a hash 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 please help and provide an explanation?

Source Link
user3479683
  • 493
  • 1
  • 5
  • 6

How to use sha256 hash in Python

I am trying to read in file of passwords. Then I am trying to compute the hash for each password and compare it to a hash 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 please help and provide an explanation?