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 trans_5C is a string, causing a TypeError #60267

Closed
AdamGlenn mannequin opened this issue Sep 27, 2012 · 3 comments
Closed

HMAC trans_5C is a string, causing a TypeError #60267

AdamGlenn mannequin opened this issue Sep 27, 2012 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@AdamGlenn
Copy link
Mannequin

AdamGlenn mannequin commented Sep 27, 2012

BPO 16063
Nosy @tiran

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 2013-10-13.18:32:23.661>
created_at = <Date 2012-09-27.17:21:13.467>
labels = ['type-bug', 'library']
title = 'HMAC trans_5C is a string, causing a TypeError'
updated_at = <Date 2013-10-13.18:32:23.660>
user = 'https://bugs.python.org/AdamGlenn'

bugs.python.org fields:

activity = <Date 2013-10-13.18:32:23.660>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2013-10-13.18:32:23.661>
closer = 'georg.brandl'
components = ['Library (Lib)']
creation = <Date 2012-09-27.17:21:13.467>
creator = 'Adam.Glenn'
dependencies = []
files = []
hgrepos = []
issue_num = 16063
keywords = []
message_count = 3.0
messages = ['171384', '171903', '172263']
nosy_count = 2.0
nosy_names = ['christian.heimes', 'Adam.Glenn']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue16063'
versions = ['Python 2.7']

@AdamGlenn
Copy link
Mannequin Author

AdamGlenn mannequin commented Sep 27, 2012

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.

@AdamGlenn AdamGlenn mannequin added the type-bug An unexpected behavior, bug, or error label Sep 27, 2012
@AdamGlenn
Copy link
Mannequin Author

AdamGlenn mannequin commented Oct 3, 2012

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
>>>

@tiran
Copy link
Member

tiran commented Oct 6, 2012

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).

@tiran tiran added the stdlib Python modules in the Lib dir label Oct 6, 2012
@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

2 participants