md5 hash problems

Chris Rebert clp at rebertia.com
Tue Sep 30 17:49:38 EDT 2008


On Tue, Sep 30, 2008 at 2:25 PM, Michele <michele at nectarine.it> wrote:
> Hi there,
> why is this code generating a problem?
>
>>>> input = open('foo.img','rb').read().decode('ISO-8859-1')
>>>> import md5
>>>> md5.new(input).hexdigest()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> UnicodeEncodeError: '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...
http://rebertia.com

>>>>
>
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list