2 Replies - 1266 Views - Last Post: 04 September 2011 - 06:28 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Generating string hash issue

Posted 04 September 2011 - 06:03 PM

I am using Python 3.2 on Windows and I have been using Google but can't seem to find an answer.

I want to create a method where I passin a variable and get a hash output to either the console or a file.
The code I have:
import hashlib

def md5hasher(data):
    md5hash = hashlib.md5()
    md5hash.update(data)
    print(md5hash.hexdigest())

md5hasher("hello world")



And the error:
Traceback (most recent call last):
  File "C:/Users/<username>/Desktop/test.py", line 8, in <module>
    md5hasher("hello world")
  File "C:/Users/<username>/Desktop/test.py", line 5, in md5hasher
    md5hash.update(data)
TypeError: Unicode-objects must be encoded before hashing



All the tutorials and the official docs show that I am doing this right but I don't get why it isn't working.

Is This A Good Question/Topic? 0
  • +

Replies To: Generating string hash issue

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,358
  • Joined: 16-October 07

Re: Generating string hash issue

Posted 04 September 2011 - 06:23 PM

Facinating. Damn Python 3... ;)

Right, in 2 is works fine:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> 
>>> def md5hasher(data):
...     md5hash = hashlib.md5()
...     md5hash.update(data)
...     print(md5hash.hexdigest())
... 
>>> md5hasher("hello world")
5eb63bbbe01eeed093cb22bb8f5acdc3
>>> 



In 3, as you found:
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> 
>>> def md5hasher(data):
...     md5hash = hashlib.md5()
...     md5hash.update(data)
...     print(md5hash.hexdigest())
... 
>>> md5hasher("hello world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in md5hasher
TypeError: Unicode-objects must be encoded before hashing



The fix is to encode the data as requested:
>>> def md5hasher(data):
...     md5hash = hashlib.md5()
...     md5hash.update(data.encode("utf8"))
...     print(md5hash.hexdigest())
... 
>>> md5hasher("hello world")
5eb63bbbe01eeed093cb22bb8f5acdc3
>>> 



Why the change? I couldn't tell you. Encoders like to work with streams of bytes, so maybe it's just someone's idea of preventing unexpected behavior. Ironic, no? Anyway, that's how you do it.

I should add, unless explicitly stated, most of the code and tutorials you'll find are still Python 2.
Was This Post Helpful? 2
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: Generating string hash issue

Posted 04 September 2011 - 06:28 PM

Thanks for the tip on how to get it to work. I was looking through the Python 3.2 docs which is why I am confused.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1