classification
Title: HMAC trans_5C is a string, causing a TypeError
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Adam.Glenn, christian.heimes
Priority: normal Keywords:

Created on 2012-09-27 17:21 by Adam.Glenn, last changed 2013-10-13 18:32 by georg.brandl. This issue is now closed.

Messages (3)
msg171384 - (view) Author: Adam Glenn (Adam.Glenn) Date: 2012-09-27 17:21
When passing 2 unicode objects to hmac.new() I'm getting "TypeError: character mapping must return integer, None or unicode" I've tried this using hashlib.sha1 and hashlib.md5 and the behavior is the same. What I think is happening is that the trans_5C join at the top of this module is a string so it's causing the type error when I try to generate the new hmac object at line 72.
msg171903 - (view) Author: Adam Glenn (Adam.Glenn) Date: 2012-10-03 19:12
I did some more testing and verified that this is a problem caused by the fact that trans_5C is a string and not unicode. It also happens when trans_36 is sent to key.translate().

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hmac
>>> hmac.new(u'key', u'msg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hmac.py", line 132, in new
    return HMAC(key, msg, digestmod)
  File "hmac.py", line 72, in __init__
    self.inner.update(key.translate(trans_36))
TypeError: character mapping must return integer, None or unicode
>>>
msg172263 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2012-10-06 23:30
That's to be expected. HMAC and all cryptographic hashing algorithms work with bytes only. Text (unicode) is neither specified by the standards nor supported. You have to convert your text to bytes with some encoding (e.g. ASCII or UTF-8).
History
Date User Action Args
2013-10-13 18:32:23georg.brandlsetstatus: pending -> closed
2012-10-06 23:30:51christian.heimessetstatus: open -> pending

nosy: + christian.heimes
messages: + msg172263

components: + Library (Lib), - None
resolution: wont fix
2012-10-03 19:12:52Adam.Glennsetmessages: + msg171903
2012-09-27 17:21:13Adam.Glenncreate