summaryrefslogtreecommitdiff
path: root/django/utils/crypto.py
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-06-07 18:08:47 +0200
committerClaude Paroz <claude@2xlibre.net>2012-06-07 18:08:47 +0200
commit4a103086d5c67fa4fcc53c106c9fdf644c742dd8 (patch)
tree3df00600c27f6369f7561c3b8ddf2f97d2d341d9 /django/utils/crypto.py
parent706fd9adc0b6587c7f96a834c757708e64fcf615 (diff)
Fixed #18269 -- Applied unicode_literals for Python 3 compatibility.
Thanks Vinay Sajip for the support of his django3 branch and Jannis Leidel for the review.
Diffstat (limited to 'django/utils/crypto.py')
-rw-r--r--django/utils/crypto.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/django/utils/crypto.py b/django/utils/crypto.py
index 0fce06012b..cc59e2e39f 100644
--- a/django/utils/crypto.py
+++ b/django/utils/crypto.py
@@ -1,6 +1,7 @@
"""
Django's standard crypto functions and utilities.
"""
+from __future__ import unicode_literals
import hmac
import struct
@@ -42,7 +43,7 @@ def salted_hmac(key_salt, value, secret=None):
# We need to generate a derived key from our base key. We can do this by
# passing the key_salt and our base key through a pseudo-random function and
# SHA1 works nicely.
- key = hashlib.sha1(key_salt + secret).digest()
+ key = hashlib.sha1((key_salt + secret).encode('utf-8')).digest()
# If len(key_salt + secret) > sha_constructor().block_size, the above
# line is redundant and could be replaced by key = key_salt + secret, since
@@ -105,7 +106,7 @@ def _long_to_bin(x, hex_format_string):
Convert a long integer into a binary string.
hex_format_string is like "%020x" for padding 10 characters.
"""
- return binascii.unhexlify(hex_format_string % x)
+ return binascii.unhexlify((hex_format_string % x).encode('ascii'))
def _fast_hmac(key, msg, digest):
@@ -113,6 +114,7 @@ def _fast_hmac(key, msg, digest):
A trimmed down version of Python's HMAC implementation
"""
dig1, dig2 = digest(), digest()
+ key = smart_str(key)
if len(key) > dig1.block_size:
key = digest(key).digest()
key += chr(0) * (dig1.block_size - len(key))