classification
Title: hmac throws TypeErrors
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, kteague, techtonik, victorstar
Priority: normal Keywords:

Created on 2009-02-16 23:34 by kteague, last changed 2011-02-05 15:33 by techtonik. This issue is now closed.

Messages (3)
msg82282 - (view) Author: Kevin Teague (kteague) Date: 2009-02-16 23:34
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?
msg85527 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-05 16:35
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.
msg127997 - (view) Author: anatoly techtonik (techtonik) Date: 2011-02-05 15:33
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
History
Date User Action Args
2011-02-05 15:33:21techtoniksetnosy: + techtonik
messages: + msg127997
2009-04-05 16:35:36georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85527

resolution: wont fix
2009-02-21 01:42:01victorstarsetnosy: + victorstar
2009-02-16 23:34:07kteaguecreate