[Tutor] forcing hashlib to has string variables

Dave Angel davea at ieee.org
Sun Sep 12 21:29:16 CEST 2010


  On 2:59 PM, Rance Hall wrote:
> 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.
Since you top-posted, I'm clipping out all the stuff that is now out of 
order.  Add your remarks after the part you're quoting.

You're in Python 3, and you have a password in a string variable pass, 
and you want to use those bytes as an argument to a function that takes 
bytes?

Convert the string to bytes.  If the string is ASCII, you can simply use 
the bytes() function.  If not, you may need to specify an encoding.

The b'xxx' syntax is only for entering in a literal byte string.  It 
gets encoded according to the source file's encoding declaration.  But 
that's not generally of any use for passwords, since you're going to ask 
the user the password, not enter it into the source.

DaveA



More information about the Tutor mailing list