测试gitnore
This commit is contained in:
@@ -4,18 +4,19 @@ Django's standard crypto functions and utilities.
|
||||
import hashlib
|
||||
import hmac
|
||||
import secrets
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.deprecation import RemovedInDjango40Warning
|
||||
from django.utils.encoding import force_bytes
|
||||
|
||||
|
||||
class InvalidAlgorithm(ValueError):
|
||||
"""Algorithm is not supported by hashlib."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def salted_hmac(key_salt, value, secret=None, *, algorithm="sha1"):
|
||||
def salted_hmac(key_salt, value, secret=None, *, algorithm='sha1'):
|
||||
"""
|
||||
Return the HMAC of 'value', using a key generated from key_salt and a
|
||||
secret (which defaults to settings.SECRET_KEY). Default algorithm is SHA1,
|
||||
@@ -32,7 +33,8 @@ def salted_hmac(key_salt, value, secret=None, *, algorithm="sha1"):
|
||||
hasher = getattr(hashlib, algorithm)
|
||||
except AttributeError as e:
|
||||
raise InvalidAlgorithm(
|
||||
"%r is not an algorithm accepted by the hashlib module." % algorithm
|
||||
'%r is not an algorithm accepted by the hashlib module.'
|
||||
% algorithm
|
||||
) from e
|
||||
# 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.
|
||||
@@ -44,10 +46,13 @@ def salted_hmac(key_salt, value, secret=None, *, algorithm="sha1"):
|
||||
return hmac.new(key, msg=force_bytes(value), digestmod=hasher)
|
||||
|
||||
|
||||
RANDOM_STRING_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
NOT_PROVIDED = object() # RemovedInDjango40Warning.
|
||||
RANDOM_STRING_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
||||
|
||||
|
||||
def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS):
|
||||
# RemovedInDjango40Warning: when the deprecation ends, replace with:
|
||||
# def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS):
|
||||
def get_random_string(length=NOT_PROVIDED, allowed_chars=RANDOM_STRING_CHARS):
|
||||
"""
|
||||
Return a securely generated random string.
|
||||
|
||||
@@ -58,7 +63,13 @@ def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS):
|
||||
* length: 12, bit length =~ 71 bits
|
||||
* length: 22, bit length =~ 131 bits
|
||||
"""
|
||||
return "".join(secrets.choice(allowed_chars) for i in range(length))
|
||||
if length is NOT_PROVIDED:
|
||||
warnings.warn(
|
||||
'Not providing a length argument is deprecated.',
|
||||
RemovedInDjango40Warning,
|
||||
)
|
||||
length = 12
|
||||
return ''.join(secrets.choice(allowed_chars) for i in range(length))
|
||||
|
||||
|
||||
def constant_time_compare(val1, val2):
|
||||
|
||||
Reference in New Issue
Block a user