Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: secrets.py
"""Generate cryptographically strong pseudo-random numbers suitable for
[0] Fix | Delete
managing secrets such as account authentication, tokens, and similar.
[1] Fix | Delete
[2] Fix | Delete
See PEP 506 for more information.
[3] Fix | Delete
https://www.python.org/dev/peps/pep-0506/
[4] Fix | Delete
[5] Fix | Delete
"""
[6] Fix | Delete
[7] Fix | Delete
__all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
[8] Fix | Delete
'token_bytes', 'token_hex', 'token_urlsafe',
[9] Fix | Delete
'compare_digest',
[10] Fix | Delete
]
[11] Fix | Delete
[12] Fix | Delete
[13] Fix | Delete
import base64
[14] Fix | Delete
import binascii
[15] Fix | Delete
[16] Fix | Delete
from hmac import compare_digest
[17] Fix | Delete
from random import SystemRandom
[18] Fix | Delete
[19] Fix | Delete
_sysrand = SystemRandom()
[20] Fix | Delete
[21] Fix | Delete
randbits = _sysrand.getrandbits
[22] Fix | Delete
choice = _sysrand.choice
[23] Fix | Delete
[24] Fix | Delete
def randbelow(exclusive_upper_bound):
[25] Fix | Delete
"""Return a random int in the range [0, n)."""
[26] Fix | Delete
if exclusive_upper_bound <= 0:
[27] Fix | Delete
raise ValueError("Upper bound must be positive.")
[28] Fix | Delete
return _sysrand._randbelow(exclusive_upper_bound)
[29] Fix | Delete
[30] Fix | Delete
DEFAULT_ENTROPY = 32 # number of bytes to return by default
[31] Fix | Delete
[32] Fix | Delete
def token_bytes(nbytes=None):
[33] Fix | Delete
"""Return a random byte string containing *nbytes* bytes.
[34] Fix | Delete
[35] Fix | Delete
If *nbytes* is ``None`` or not supplied, a reasonable
[36] Fix | Delete
default is used.
[37] Fix | Delete
[38] Fix | Delete
>>> token_bytes(16) #doctest:+SKIP
[39] Fix | Delete
b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b'
[40] Fix | Delete
[41] Fix | Delete
"""
[42] Fix | Delete
if nbytes is None:
[43] Fix | Delete
nbytes = DEFAULT_ENTROPY
[44] Fix | Delete
return _sysrand.randbytes(nbytes)
[45] Fix | Delete
[46] Fix | Delete
def token_hex(nbytes=None):
[47] Fix | Delete
"""Return a random text string, in hexadecimal.
[48] Fix | Delete
[49] Fix | Delete
The string has *nbytes* random bytes, each byte converted to two
[50] Fix | Delete
hex digits. If *nbytes* is ``None`` or not supplied, a reasonable
[51] Fix | Delete
default is used.
[52] Fix | Delete
[53] Fix | Delete
>>> token_hex(16) #doctest:+SKIP
[54] Fix | Delete
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
[55] Fix | Delete
[56] Fix | Delete
"""
[57] Fix | Delete
return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
[58] Fix | Delete
[59] Fix | Delete
def token_urlsafe(nbytes=None):
[60] Fix | Delete
"""Return a random URL-safe text string, in Base64 encoding.
[61] Fix | Delete
[62] Fix | Delete
The string has *nbytes* random bytes. If *nbytes* is ``None``
[63] Fix | Delete
or not supplied, a reasonable default is used.
[64] Fix | Delete
[65] Fix | Delete
>>> token_urlsafe(16) #doctest:+SKIP
[66] Fix | Delete
'Drmhze6EPcv0fN_81Bj-nA'
[67] Fix | Delete
[68] Fix | Delete
"""
[69] Fix | Delete
tok = token_bytes(nbytes)
[70] Fix | Delete
return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')
[71] Fix | Delete
[72] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function