
    tf                         d Z ddlZddlZddlZ eedd          Zej        Z G d de	          Z
 G d de	          Z G d d	          Z e            Zej        Zej        Z[g d
ZdS )zQ
Cryptographically secure random implementation, with fallback on normal random.
    Ngetrandbitsc                       e Zd ZdZdS )SecureRandomNotAvailablezD
    Exception raised when no secure random algorithm is found.
    N__name__
__module____qualname____doc__     W/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/python/randbytes.pyr   r                 r   r   c                       e Zd ZdZdS )SourceNotAvailablezQ
    Internal exception used when a specific random source is not available.
    Nr   r   r   r   r   r      r   r   r   c                       e Zd ZdZdZeZdedefdZddede	defdZ
dedefd	Zej        Z ed
d
          ZdedefdZdedefdZdS )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   nbytesreturnc                 |    	 t          j        |          S # t          t          f$ r}t	          |          d}~ww xY w)zO
        Wrapper around C{os.urandom} that cleanly manage its absence.
        N)osurandomAttributeErrorNotImplementedErrorr   )selfr   es      r   
_osUrandomzRandomFactory._osUrandom-   sJ    	(:f%%% 34 	( 	( 	($Q'''	(s    ;6;Ffallbackc                     	 |                      |          S # t          $ r Y nw xY w|r1t          j        dt          d           |                     |          S t          d          )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   r   s      r   secureRandomzRandomFactory.secureRandom6   s    	??6***! 	 	 	D	  		PMM'	    &&v...*+NOOO    
$$c                     | j         2|                      |dz            }d|dz  z  |z  }t          |          S t          d          )z3
        Wrapper around C{os.getrandbits}.
        N   z%%0%dxr   z#random.getrandbits is not available)r   _fromhexr   )r   r   nhexBytess       r   	_randBitszRandomFactory._randBitsS   sT     '  !,,A FQJ/14HH%%% !FGGGr   r   c                 `     d                      fdt          |          D                       S )z6
        Wrapper around the C{random} module.
        r   c                 ^    g | ])}t          t          j        j                  g          *S r   )bytesrandomchoice_BYTES).0ir   s     r   
<listcomp>z-RandomFactory._randModule.<locals>.<listcomp>d   s0    TTTdk : :;<<TTTr   )joinranger   r   s   ` r   _randModulezRandomFactory._randModule`   s2     xxTTTTeFmmTTTUUUr   c                 x    	 |                      |          S # t          $ r Y nw xY w|                     |          S )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-   r   r:   r9   s     r   r%   zRandomFactory.insecureRandomf   sM    	>>&)))! 	 	 	D	'''r'   N)F)r   r   r	   r
   randomSourcesr   intr0   r   boolr&   r-   	maketrans
_maketransr3   r:   r%   r   r   r   r   r      s         MK( ( ( ( ( (P P3 P$ P5 P P P P:H H H H H H JZS!!FV# V% V V V V(S (U ( ( ( ( ( (r   r   )r&   r%   r   )r
   r   r1   r"   getattrr   r0   fromhexr*   RuntimeErrorr   r   r   factoryr&   r%   __all__r   r   r   <module>rF      s   
 
 
			  gfmT22=    |          U( U( U( U( U( U( U( U(p -//#' I
H
Hr   