File: //usr/lib/python3/dist-packages/twisted/python/__pycache__/randbytes.cpython-310.pyc
o
�b�
� @ s| d Z ddlZddlZddlZeedd�ZejZG dd� de �Z
G dd� de �ZG dd � d �Ze� Z
e
jZe
jZ[
g d
�ZdS )zQ
Cryptographically secure random implementation, with fallback on normal random.
� N�getrandbitsc @ � e Zd ZdZdS )�SecureRandomNotAvailablezD
Exception raised when no secure random algorithm is found.
N��__name__�
__module__�__qualname__�__doc__� r
r
�:/usr/lib/python3/dist-packages/twisted/python/randbytes.pyr � r c @ r )�SourceNotAvailablezQ
Internal exception used when a specific random source is not available.
Nr r
r
r
r r
r r
c @ sR e Zd ZdZdZeZdd� Zddd�Zdd � Ze j
Zed
d
�Zdd� Z
d
d� ZdS )�
RandomFactoryz�
Factory providing L{secureRandom} and L{insecureRandom} methods.
You shouldn't have to instantiate this class, use the module level
functions instead: it is an implementation detail and could be removed or
changed arbitrarily.
r
c
C s2 zt �|�W S ttfy } zt|��d}~ww )zO
Wrapper around C{os.urandom} that cleanly manage its absence.
N)�os�urandom�AttributeError�NotImplementedErrorr
)�self�nbytes�er
r
r �
_osUrandom- s ��zRandomFactory._osUrandomFc C sF z| � |�W S ty Y nw |rtjdtdd� | �|�S td��)ak
Return a number of secure random bytes.
@param nbytes: number of bytes to generate.
@type nbytes: C{int}
@param fallback: Whether the function should fallback on non-secure
random or not. Default to C{False}.
@type fallback: C{bool}
@return: a string of random bytes.
@rtype: C{str}
zPurandom unavailable - proceeding with non-cryptographically secure random source� )�category�
stacklevelz!No secure random source available)r r
�warnings�warn�RuntimeWarning�insecureRandomr )r r �fallbackr
r
r �secureRandom6 s
��
zRandomFactory.secureRandomc C s8 | j dur| � |d �}d|d | }t|�S td��)z3
Wrapper around C{os.getrandbits}.
N� z%%0%dxr z#random.getrandbits is not available)r �_fromhexr
)r r �n�hexBytesr
r
r � _randBitsS s
zRandomFactory._randBits� c s d� � fdd�t|�D ��S )z6
Wrapper around the C{random} module.
r% c s g | ]}t t�� j�g��qS r
)�bytes�random�choice�_BYTES)�.0�i�r r
r �
<listcomp>d s z-RandomFactory._randModule.<locals>.<listcomp>)�join�range)r r r
r, r �_randModule` s zRandomFactory._randModulec C s4 dD ]}z
t | |�|�W S ty Y qw dS )z�
Return a number of non secure random bytes.
@param nbytes: number of bytes to generate.
@type nbytes: C{int}
@return: a string of random bytes.
@rtype: C{str}
)r$ r0 N)�getattrr
)r r �srcr
r
r r f s
��zRandomFactory.insecureRandomN)F)r r r r �
randomSourcesr r r r$ r&