Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashlib.md5() TypeError: Unicode-objects must be encoded before hashing

I am new to coding and have ran into a problem trying to encode a string.

>>> import hashlib >>> a = hashlib.md5() >>> a.update('hi') Traceback (most recent call last):   File "<pyshell#22>", line 1, in <module>     a.update('hi') TypeError: Unicode-objects must be encoded before hashing >>> a.digest() b'\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~' 

Is (a) now considered to be encoded?

Second question: When I run the same code above in a script I get this error:

import hashlib a = hashlib.md5() a.update('hi') a.digest() 

Traceback (most recent call last): File "C:/Users/User/Desktop/Logger/Encoding practice.py", line 3, in a.update('hi') TypeError: Unicode-objects must be encoded before hashing

Why is the code working in the shell and not the script? I am working with Windows and Python 3.4

Thanks.

like image 551
Shahab Avatar asked Dec 17 '14 06:12

Shahab


People also ask

How do you encode before hashing?

The Python "TypeError: Strings must be encoded before hashing" occurs when we pass a string to a hashing algorithm. To solve the error, use the encode() method to encode the string to a bytes object, e.g. my_str. encode('utf-8') .

How do I use MD5 in Python?

Use the MD5 Algorithm in Python To obtain the hash value, use the digest() method, which returns a bytes object digest of the data fed to the hash object. Similar to the digest() method, you can also use hexdigest() , which returns a string object of the digest containing only hexadecimal digits.

What is Hexdigest in Python?

hexdigest() : the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.


2 Answers

Since you are encoding simple strings I deduce that you are running Python 3 where all strings are unicode objects, you have two options:

  1. Provide an encoding for the strings, e.g.: "Nobody inspects".encode('utf-8')
  2. Use binary strings as shown in the manuals:

    m.update(b"Nobody inspects") m.update(b" the spammish repetition") 

The reason for the differing behaviour in the script to the shell is that the script stops on the error whereas in the shell the last line is a separate command but still not doing what you wish it to because of the previous error.

like image 37
Steve Barnes Avatar answered Sep 25 '22 06:09

Steve Barnes


The solution I've found is to simply encode the data right away in the line where you're hashing it:

hashlib.sha256("a".encode('utf-8')).hexdigest()

It worked for me, hope it helps!

like image 190
Matej Butković Avatar answered Sep 23 '22 06:09

Matej Butković