Skip to content

Instantly share code, notes, and snippets.

@nvie
Created March 4, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nvie/5083455 to your computer and use it in GitHub Desktop.
Save nvie/5083455 to your computer and use it in GitHub Desktop.
The following code crashes when ran on Django==1.5, but it works fine on Django==1.4.3. (Both py27.)
$ python minimal_example.py
Traceback (most recent call last):
File "minimal_example.py", line 7, in <module>
s.sign('foo')
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 175, in sign
return str('%s%s%s') % (value, self.sep, self.signature(value))
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 169, in signature
signature = base64_hmac(self.salt + 'signer', value, self.key)
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 75, in base64_hmac
return b64_encode(salted_hmac(salt, value, key).digest())
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/utils/crypto.py", line 48, in salted_hmac
key = hashlib.sha1((key_salt + secret).encode('utf-8')).digest()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)
from django.conf import settings
from django.core.signing import Signer
settings.SECRET_KEY = b'\xe7' # binary key with non-ASCII chars
s = Signer()
print(s.sign('foo'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment