[Tutor] forcing hashlib to has string variables

Dave Angel davea at ieee.org
Sun Sep 12 21:23:36 CEST 2010



On 2:59 PM, Rance Hall 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?
>
What version of Python are you using, and what version of Python is "the 
documentation" written for?

In version 2.x, a string is already a series of bytes.

DaveA



More information about the Tutor mailing list