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
Comments
In Python 2.6 a performance optimization was added to the hmac module 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 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 |
Python 3 behavior is correct. Since HMAC operates on bytes, not text, |
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. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: