summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2021-05-05 23:28:08 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-05-07 11:57:40 +0200
commit028f10fac651292edf8d50e013b16a52862e4c08 (patch)
treedacf9d5fcd6e337a213bbd2a942a7894768955e6 /django
parentc4ee3b208a2c95a5102b5e4fa789b10f8ee29b84 (diff)
Fixed #32712 -- Deprecated django.utils.baseconv module.
Diffstat (limited to 'django')
-rw-r--r--django/core/signing.py31
-rw-r--r--django/utils/baseconv.py10
2 files changed, 38 insertions, 3 deletions
diff --git a/django/core/signing.py b/django/core/signing.py
index 0a566ff47d..5ee19a9336 100644
--- a/django/core/signing.py
+++ b/django/core/signing.py
@@ -40,13 +40,13 @@ import time
import zlib
from django.conf import settings
-from django.utils import baseconv
from django.utils.crypto import constant_time_compare, salted_hmac
from django.utils.encoding import force_bytes
from django.utils.module_loading import import_string
from django.utils.regex_helper import _lazy_re_compile
_SEP_UNSAFE = _lazy_re_compile(r'^[A-z0-9-_=]*$')
+BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
class BadSignature(Exception):
@@ -59,6 +59,31 @@ class SignatureExpired(BadSignature):
pass
+def b62_encode(s):
+ if s == 0:
+ return '0'
+ sign = '-' if s < 0 else ''
+ s = abs(s)
+ encoded = ''
+ while s > 0:
+ s, remainder = divmod(s, 62)
+ encoded = BASE62_ALPHABET[remainder] + encoded
+ return sign + encoded
+
+
+def b62_decode(s):
+ if s == '0':
+ return 0
+ sign = 1
+ if s[0] == '-':
+ s = s[1:]
+ sign = -1
+ decoded = 0
+ for digit in s:
+ decoded = decoded * 62 + BASE62_ALPHABET.index(digit)
+ return sign * decoded
+
+
def b64_encode(s):
return base64.urlsafe_b64encode(s).strip(b'=')
@@ -187,7 +212,7 @@ class Signer:
class TimestampSigner(Signer):
def timestamp(self):
- return baseconv.base62.encode(int(time.time()))
+ return b62_encode(int(time.time()))
def sign(self, value):
value = '%s%s%s' % (value, self.sep, self.timestamp())
@@ -200,7 +225,7 @@ class TimestampSigner(Signer):
"""
result = super().unsign(value)
value, timestamp = result.rsplit(self.sep, 1)
- timestamp = baseconv.base62.decode(timestamp)
+ timestamp = b62_decode(timestamp)
if max_age is not None:
if isinstance(max_age, datetime.timedelta):
max_age = max_age.total_seconds()
diff --git a/django/utils/baseconv.py b/django/utils/baseconv.py
index c43d9e5a9d..21f1fb3b91 100644
--- a/django/utils/baseconv.py
+++ b/django/utils/baseconv.py
@@ -1,3 +1,4 @@
+# RemovedInDjango50Warning
# Copyright (c) 2010 Guilherme Gondim. All rights reserved.
# Copyright (c) 2009 Simon Willison. All rights reserved.
# Copyright (c) 2002 Drew Perttula. All rights reserved.
@@ -36,6 +37,15 @@ Sample usage::
-1234
"""
+import warnings
+
+from django.utils.deprecation import RemovedInDjango50Warning
+
+warnings.warn(
+ 'The django.utils.baseconv module is deprecated.',
+ category=RemovedInDjango50Warning,
+ stacklevel=2,
+)
BASE2_ALPHABET = '01'
BASE16_ALPHABET = '0123456789ABCDEF'