
    \d                         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__     8lib/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 ZddZd Ze	j
        Z edd          Zd Z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   c                 |    	 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   )selfnbyteses      r   
_osUrandomzRandomFactory._osUrandom-   sJ    	(:f%%% 34 	( 	( 	($Q'''	(s    ;6;Fc                     	 |                      |          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   fallbacks      r   secureRandomzRandomFactory.secureRandom6   s    	??6***! 	 	 	D	  		PMM'	    &&v...*+NOOOs    
$$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                 f    dD ]-}	  t          | |          |          c S # t          $ r Y *w xY wd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+   r7   N)getattrr   )r   r   srcs      r   r#   zRandomFactory.insecureRandomf   sb     0 	 	C)wtS))&11111%   	 	s   !
..N)F)r   r   r	   r
   randomSourcesr   r   r%   r+   r.   	maketrans
_maketransr1   r7   r#   r   r   r   r   r      s          MK( ( (P P P P:H H H JZS!!FV V V    r   r   )r%   r#   r   )r
   r   r/   r    r9   r   r.   fromhexr(   RuntimeErrorr   r   r   factoryr%   r#   __all__r   r   r   <module>rB      s   
 
 
			  gfmT22=    |          U U U U U U U Up -//#' I
H
Hr   