md5 hash problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele

    md5 hash problems

    Hi there,
    why is this code generating a problem?
    >>input = open('foo.img', 'rb').read().de code('ISO-8859-1')
    >>import md5
    >>md5.new(input ).hexdigest()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xdc' in
    position 6:
    ordinal not in range(128)
    >>>
    Thank you.
  • Chris Rebert

    #2
    Re: md5 hash problems

    On Tue, Sep 30, 2008 at 2:25 PM, Michele <michele@nectar ine.itwrote:
    Hi there,
    why is this code generating a problem?
    >
    >>>input = open('foo.img', 'rb').read().de code('ISO-8859-1')
    >>>import md5
    >>>md5.new(inpu t).hexdigest()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xdc' in
    position 6:
    ordinal not in range(128)
    You're trying to run md5 over a unicode string, but I'm pretty sure
    it's only defined for bytes, and when Python tries to autoconvert the
    unicode to bytes (by encoding it in ASCII), this fails because you
    particular unicode string contains non-ASCII characters. Basically
    make sure to pass md5.new() bytes and not unicode, either by removing
    the call to .decode(), or calling .encode() on the unicode object
    before passing it to md5.new()

    Regards,
    Chris
    --
    Follow the path of the Iguana...

    Comment

    Working...