MCPcopy
hub / github.com/django/django / get_random_string

Function get_random_string

django/utils/crypto.py:66–77  ·  view source on GitHub ↗

Return a securely generated random string. The bit length of the returned value can be calculated with the formula: log_2(len(allowed_chars)^length) For example, with default `allowed_chars` (26+26+10), this gives: * length: 12, bit length =~ 71 bits * length: 22,

(length, allowed_chars=RANDOM_STRING_CHARS)

Source from the content-addressed store, hash-verified

64
65
66def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS):
67 """
68 Return a securely generated random string.
69
70 The bit length of the returned value can be calculated with the formula:
71 log_2(len(allowed_chars)^length)
72
73 For example, with default `allowed_chars` (26+26+10), this gives:
74 * length: 12, bit length =~ 71 bits
75 * length: 22, bit length =~ 131 bits
76 """
77 return "".join(secrets.choice(allowed_chars) for i in range(length))
78
79
80def constant_time_compare(val1, val2):

Callers 15

_get_new_session_keyMethod · 0.90
_aget_new_session_keyMethod · 0.90
verify_passwordFunction · 0.90
make_passwordFunction · 0.90
saltMethod · 0.90
_get_new_csrf_stringFunction · 0.90
get_alternative_nameMethod · 0.90
get_random_secret_keyFunction · 0.90
__init__Method · 0.90
enqueueMethod · 0.90
enqueueMethod · 0.90
_test_database_passwdMethod · 0.90

Calls 2

joinMethod · 0.45
choiceMethod · 0.45

Tested by 3

test_session_fallbackMethod · 0.72