Projet

Général

Profil

0001-python3-use-binascii-s-hexadecimal-encoding-hexlify-.patch

Paul Marillonnet, 04 avril 2019 18:14

Télécharger (4,02 ko)

Voir les différences:

Subject: [PATCH] python3: use binascii's hexadecimal encoding "hexlify"
 (#31163)

 src/authentic2/hashers.py    | 17 +++++++++++------
 src/authentic2/middleware.py |  6 +++++-
 2 files changed, 16 insertions(+), 7 deletions(-)
src/authentic2/hashers.py
1 1
import hashlib
2 2
import math
3 3
import base64
4
from binascii import hexlify
4 5
from collections import OrderedDict
5 6

  
6 7
from django.contrib.auth import hashers
7 8
from django.utils.crypto import constant_time_compare
8 9
from django.utils.translation import ugettext_noop as _
9
from django.utils.encoding import force_bytes
10
from django.utils.encoding import force_bytes, force_text
10 11
from django.contrib.auth.hashers import make_password
11 12

  
12 13

  
......
138 139
        algo_name, salt_offset, hex_encode = OPENLDAP_ALGO_MAPPING[algo]
139 140
        salt, password = (password[salt_offset:], password[:salt_offset]) if salt_offset else ('', password)
140 141
        if hex_encode:
141
            password = password.encode('hex')
142
        return '%s$%s$%s' % (algo_name, salt.encode('hex'), password)
142
            password = force_text(hexlify(password), encoding='ascii')
143
        salt = force_text(hexlify(salt), encoding='ascii')
144
        return '%s$%s$%s' % (algo_name, salt, password)
143 145
    else:
144 146
        return make_password(password)
145 147

  
......
149 151
        assert password
150 152
        assert '$' not in salt
151 153
        hash = self.digest(force_bytes(password + salt)).hexdigest()
152
        return "%s$%s$%s" % (self.algorithm, salt.encode('hex'), hash)
154
        salt = force_text(hexlify(salt), encoding='ascii')
155
        return "%s$%s$%s" % (self.algorithm, salt, hash)
153 156

  
154 157
    def verify(self, password, encoded):
155 158
        algorithm, salt, hash = encoded.split('$', 2)
......
199 202
        assert password
200 203
        assert '$' not in salt
201 204
        hash = self.digest(force_bytes(password + salt)).hexdigest()
202
        return "%s$md5$%s$%s" % (self.algorithm, salt.encode('hex'), hash)
205
        salt = force_text(hexlify(salt), encoding='ascii')
206
        return "%s$md5$%s$%s" % (self.algorithm, salt, hash)
203 207

  
204 208
    def verify(self, password, encoded):
205 209
        algorithm, subalgo, salt, hash = encoded.split('$', 3)
......
222 226
                h, salt = encoded.split(':', 1)
223 227
            else:
224 228
                h, salt = encoded, ''
225
            return '%s$md5$%s$%s' % (cls.algorithm, salt.encode('hex'), h)
229
            salt = force_text(hexlify(salt), encoding='ascii')
230
            return '%s$md5$%s$%s' % (cls.algorithm, salt, h)
226 231

  
227 232
    @classmethod
228 233
    def to_joomla(cls, encoded):
src/authentic2/middleware.py
8 8
except ImportError:
9 9
    threading = None
10 10

  
11
from binascii import hexlify
11 12
from django.conf import settings
12 13
from django.contrib import messages
14
from django.utils.encoding import force_text
13 15
from django.utils.translation import ugettext as _
14 16
from django.utils.six.moves.urllib import parse as urlparse
15 17
from django.shortcuts import render
......
130 132
                # Use Mersennes Twister rng, no need for a cryptographic grade
131 133
                # rng in this case
132 134
                random_id = random.getrandbits(32)
133
                request.request_id = struct.pack('I', random_id).encode('hex')
135
                request.request_id = force_text(
136
                    hexlify(struct.pack('I', random_id)),
137
                    encoding='ascii')
134 138

  
135 139
class StoreRequestMiddleware(object):
136 140
    collection = {}
137
-