This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bad examples in hashlib documentation
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: beazley, benjamin.peterson, georg.brandl
Priority: release blocker Keywords:

Created on 2008-12-29 19:54 by beazley, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg78480 - (view) Author: David M. Beazley (beazley) Date: 2008-12-29 19:54
The hashlib documentation has incorrect examples showing the use of the 
hexdigest() method:

>>> hashlib.sha224(b"Nobody inspects the spammish 
repetition").hexdigest()
b'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
>>>

and this one

>>> h = hashlib.new('ripemd160')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
b'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
>>>

However, the result of h.hexdigest() is of type 'str', not bytes. Actual 
output:

>>> h.hexdigest()
'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
>>> 

Sure would be nice if that string of hex digits was easy to decode back 
into a binary string.  

>>> import binascii
>>> b = binascii.a2b_hex(h.hexdigest())
>>>

Hmmm. So *SOME* of the functions in binascii do accept Unicode strings. 
See Issue 4470 :-).
msg78481 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-29 20:27
Shouldn't hash algorithms give bytes results anyway?
msg78482 - (view) Author: David M. Beazley (beazley) Date: 2008-12-29 20:33
The digest() method of hashes does produce bytes (correct).   The 
hexdigest() method produces a string, but it is also shown as producing 
bytes in the examples.
msg78484 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-29 20:52
Fixed in r68027.
History
Date User Action Args
2022-04-11 14:56:43adminsetgithub: 49021
2008-12-29 20:52:28benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg78484
2008-12-29 20:33:33beazleysetmessages: + msg78482
2008-12-29 20:27:08benjamin.petersonsetpriority: release blocker
nosy: + benjamin.peterson
messages: + msg78481
components: + Library (Lib), - Documentation
2008-12-29 19:54:12beazleycreate