[Tutor] forcing hashlib to has string variables

Rance Hall ranceh at gmail.com
Sun Sep 12 20:54:39 CEST 2010


Luke:

On python3.1 I get the following error using your (untested) two line snippet:

TypeError: Unicode-objects must be encoded before hashing

If I add the b back into the mix, I get a hash with no error messages.

But I still can't quite figure out how to get the variable contents
into the hashing function.

On Sun, Sep 12, 2010 at 1:47 PM, Luke Paireepinart
<rabidpoobear at gmail.com> wrote:
> This is how I use it (untested)
> Import hashlib
> Print hashlib.md5("somestr").hexdigest()
>
> Works fine without using binary string.
>
>
> On Sep 12, 2010, at 1:19 PM, Rance Hall <ranceh at gmail.com> wrote:
>
>> Everybody knows you don't store plain text passwords in a database,
>> you store hashes instead
>>
>> consider:
>>
>> userpass = getpass.getpass("User password? ")
>>
>> encuserpass = hashlib.md5()
>>
>> encuserpass.update(userpass)
>>
>> del userpass
>>
>>
>> Now the documentation clearly states that if you are hashing a string
>> you need to covert it to bytes first with a line like this:
>>
>> encuserpass.update(b"text string here")
>>
>> The "b" in this syntax is a shortcut to converting the string to bytes
>> for hasing purposes.
>>
>> which means that the first code snippet fails, since I didnt convert
>> the variable contents to bytes instead of text.
>>
>> I didn't see an example that addresses hashing the string contents of
>> a variable.
>>
>> Whats missing in the above example that makes hashing the contents of
>> a string variable work?
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list