python3: use binascii's hexadecimal encoding "hexlify" (#31163)

This commit is contained in:
Paul Marillonnet 2019-01-31 12:57:58 +01:00
parent 00db0f3350
commit f7d2fb10df
2 changed files with 16 additions and 7 deletions

View File

@ -1,12 +1,13 @@
import hashlib
import math
import base64
from binascii import hexlify
from collections import OrderedDict
from django.contrib.auth import hashers
from django.utils.crypto import constant_time_compare
from django.utils.translation import ugettext_noop as _
from django.utils.encoding import force_bytes
from django.utils.encoding import force_bytes, force_text
from django.contrib.auth.hashers import make_password
@ -138,8 +139,9 @@ def olap_password_to_dj(password):
algo_name, salt_offset, hex_encode = OPENLDAP_ALGO_MAPPING[algo]
salt, password = (password[salt_offset:], password[:salt_offset]) if salt_offset else ('', password)
if hex_encode:
password = password.encode('hex')
return '%s$%s$%s' % (algo_name, salt.encode('hex'), password)
password = force_text(hexlify(password), encoding='ascii')
salt = force_text(hexlify(salt), encoding='ascii')
return '%s$%s$%s' % (algo_name, salt, password)
else:
return make_password(password)
@ -149,7 +151,8 @@ class OpenLDAPPasswordHasher(CommonPasswordHasher):
assert password
assert '$' not in salt
hash = self.digest(force_bytes(password + salt)).hexdigest()
return "%s$%s$%s" % (self.algorithm, salt.encode('hex'), hash)
salt = force_text(hexlify(salt), encoding='ascii')
return "%s$%s$%s" % (self.algorithm, salt, hash)
def verify(self, password, encoded):
algorithm, salt, hash = encoded.split('$', 2)
@ -199,7 +202,8 @@ class JoomlaPasswordHasher(CommonPasswordHasher):
assert password
assert '$' not in salt
hash = self.digest(force_bytes(password + salt)).hexdigest()
return "%s$md5$%s$%s" % (self.algorithm, salt.encode('hex'), hash)
salt = force_text(hexlify(salt), encoding='ascii')
return "%s$md5$%s$%s" % (self.algorithm, salt, hash)
def verify(self, password, encoded):
algorithm, subalgo, salt, hash = encoded.split('$', 3)
@ -222,7 +226,8 @@ class JoomlaPasswordHasher(CommonPasswordHasher):
h, salt = encoded.split(':', 1)
else:
h, salt = encoded, ''
return '%s$md5$%s$%s' % (cls.algorithm, salt.encode('hex'), h)
salt = force_text(hexlify(salt), encoding='ascii')
return '%s$md5$%s$%s' % (cls.algorithm, salt, h)
@classmethod
def to_joomla(cls, encoded):

View File

@ -8,8 +8,10 @@ try:
except ImportError:
threading = None
from binascii import hexlify
from django.conf import settings
from django.contrib import messages
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _
from django.utils.six.moves.urllib import parse as urlparse
from django.shortcuts import render
@ -130,7 +132,9 @@ class RequestIdMiddleware(object):
# Use Mersennes Twister rng, no need for a cryptographic grade
# rng in this case
random_id = random.getrandbits(32)
request.request_id = struct.pack('I', random_id).encode('hex')
request.request_id = force_text(
hexlify(struct.pack('I', random_id)),
encoding='ascii')
class StoreRequestMiddleware(object):
collection = {}