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

hmac throws TypeErrors #49535

Closed
kteague mannequin opened this issue Feb 16, 2009 · 3 comments
Closed

hmac throws TypeErrors #49535

kteague mannequin opened this issue Feb 16, 2009 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@kteague
Copy link
Mannequin

kteague mannequin commented Feb 16, 2009

BPO 5285
Nosy @birkenfeld

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 = None
closed_at = <Date 2009-04-05.16:35:36.949>
created_at = <Date 2009-02-16.23:34:07.576>
labels = ['type-bug', 'library']
title = 'hmac throws TypeErrors'
updated_at = <Date 2011-02-05.15:33:21.080>
user = 'https://bugs.python.org/kteague'

bugs.python.org fields:

activity = <Date 2011-02-05.15:33:21.080>
actor = 'techtonik'
assignee = 'none'
closed = True
closed_date = <Date 2009-04-05.16:35:36.949>
closer = 'georg.brandl'
components = ['Library (Lib)']
creation = <Date 2009-02-16.23:34:07.576>
creator = 'kteague'
dependencies = []
files = []
hgrepos = []
issue_num = 5285
keywords = []
message_count = 3.0
messages = ['82282', '85527', '127997']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'techtonik', 'kteague', 'victorstar']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue5285'
versions = ['Python 2.6', 'Python 3.0']

@kteague
Copy link
Mannequin Author

kteague mannequin commented Feb 16, 2009

In Python 2.6 a performance optimization was added to the hmac module
(http://bugs.python.org/issue1618455). This appears to have the
unintended consequence of no longer accepting unicode strings as input.

Python 2.5.1 (r251:54863, Feb  4 2008, 21:48:13) 
>>> import hmac
>>> hmac.new(u'key','msg')
<hmac.HMAC instance at 0x6e440>

Python 2.6.1 (r261:67515, Dec  9 2008, 14:49:30) 
>>> import hmac
>>> hmac.new(u'key','msg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/Users/kteague/buildouts/shared/python-2.6.1/lib/python2.6/hmac.py",
line 133, in new
    return HMAC(key, msg, digestmod)
  File
"/Users/kteague/buildouts/shared/python-2.6.1/lib/python2.6/hmac.py",
line 72, in __init__
    self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode

This change is also reflected in Python 3.0 as the hmac module only
accepts data as input (althought the docs still mention 'strings').

Python 3.0.1 (r301:69556, Feb 16 2009, 14:38:14) 
>>> import hmac
>>> hmac.new('key','msg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/Users/kteague/buildouts/shared/python-3.0.1/lib/python3.0/hmac.py",
line 140, in new
    return HMAC(key, msg, digestmod)
  File
"/Users/kteague/buildouts/shared/python-3.0.1/lib/python3.0/hmac.py",
line 43, in __init__
    raise TypeError("expected bytes, but got %r" % type(key).__name__)
TypeError: expected bytes, but got 'str'
>>> hmac.new(b'key',b'msg')
<hmac.HMAC object at 0x3a1810>

This module should likely accept both string and unicode (Python 2) or
text and data (Python 3) and handle the conversion internally?

@kteague kteague mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 16, 2009
@birkenfeld
Copy link
Member

Python 3 behavior is correct. Since HMAC operates on bytes, not text,
only bytes are accepted. In Python 2, the acceptance of Unicode strings
is more an accident than a feature.

@techtonik
Copy link
Mannequin

techtonik mannequin commented Feb 5, 2011

This damn bug ruined my day. MoinMoin couldn't reset password on many outdated wikies < 1.8.2 (including Python's one probably), because of it.

http://moinmo.in/MoinMoinBugs/1.8_ResetPasswordError

@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
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant