Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad examples in hashlib documentation #49021

Closed
beazley mannequin opened this issue Dec 29, 2008 · 4 comments
Closed

Bad examples in hashlib documentation #49021

beazley mannequin opened this issue Dec 29, 2008 · 4 comments
Assignees
Labels
release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@beazley
Copy link
Mannequin

beazley mannequin commented Dec 29, 2008

BPO 4771
Nosy @birkenfeld, @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/birkenfeld'
closed_at = <Date 2008-12-29.20:52:28.076>
created_at = <Date 2008-12-29.19:54:12.423>
labels = ['type-bug', 'library', 'release-blocker']
title = 'Bad examples in hashlib documentation'
updated_at = <Date 2008-12-29.20:52:28.074>
user = 'https://bugs.python.org/beazley'

bugs.python.org fields:

activity = <Date 2008-12-29.20:52:28.074>
actor = 'benjamin.peterson'
assignee = 'georg.brandl'
closed = True
closed_date = <Date 2008-12-29.20:52:28.076>
closer = 'benjamin.peterson'
components = ['Library (Lib)']
creation = <Date 2008-12-29.19:54:12.423>
creator = 'beazley'
dependencies = []
files = []
hgrepos = []
issue_num = 4771
keywords = []
message_count = 4.0
messages = ['78480', '78481', '78482', '78484']
nosy_count = 3.0
nosy_names = ['georg.brandl', 'beazley', 'benjamin.peterson']
pr_nums = []
priority = 'release blocker'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4771'
versions = ['Python 3.0']

@beazley
Copy link
Mannequin Author

beazley mannequin commented Dec 29, 2008

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 bpo-4470 :-).

@beazley beazley mannequin added the docs Documentation in the Doc dir label Dec 29, 2008
@beazley beazley mannequin assigned birkenfeld Dec 29, 2008
@beazley beazley mannequin added the type-bug An unexpected behavior, bug, or error label Dec 29, 2008
@benjaminp
Copy link
Contributor

Shouldn't hash algorithms give bytes results anyway?

@benjaminp benjaminp added stdlib Python modules in the Lib dir release-blocker and removed docs Documentation in the Doc dir labels Dec 29, 2008
@beazley
Copy link
Mannequin Author

beazley mannequin commented Dec 29, 2008

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.

@benjaminp
Copy link
Contributor

Fixed in r68027.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants