[Tutor] hashlib problems

Peter Otten __peter__ at web.de
Fri Sep 10 20:40:27 CEST 2010


Rance Hall wrote:

> Im wanting to use the builtin hashlib functions to encrypt passwords
> before storing them in a database.
> 
> According to documentation on the python.org site it should be as simple
> as
> 
> import hashlib
> 
> hashname = hashlib.sha234  (or whatever other hash method you want
> like md5 or whatever)
> 
> hashname.update(b"test")  the b is for byte since the documentation
> states that hashlib does not accept strings
> 
> hashname.hexdigest() or digest() will give you the hash encrypted
> value.  (hexdigest will encode the output in hex for use in email and
> string db storage)
> 
> 
> My problem is that hashname.update results in a:
> 
> AttributeError:  'built_in_function_or_method' object has no attribute
> 'update'
> 
> I get this error in python 2 and python 3
> 
> I get it on multiple operating systems, so I dont understand whats
> going wrong.  Either there is a bug in the module hashlib, or the
> module changed and the docs don't keep up.

Neither
 
> Either way I can not encrypt the password of my project for new users
> till I figure out whats going on.

hashlib.sha224 is indeed a function:

>>> f = hashlib.sha224
>>> f
<built-in function openssl_sha224>

You have to invoke it to get a hash object:

>>> g = f()
>>> g
<sha224 HASH object @ 0x229dbf0>

Complete working example:

>>> import hashlib
>>> h = hashlib.sha224() # note the parens
>>> h.update(b"test")
>>> h.hexdigest()
'90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809'

Look here for another one:

http://docs.python.org/dev/py3k/library/hashlib.html

Peter



More information about the Tutor mailing list