o
    b                     @   s|   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 de	Z
G dd de	ZG d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__ 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 Zd ZdZdZeZdd ZdddZdd	 Ze	j
Zed
d
Zdd Zd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              
   C   s2   zt |W S  ttfy } zt|d}~ww )zO
        Wrapper around C{os.urandom} that cleanly manage its absence.
        N)osurandomAttributeErrorNotImplementedErrorr   )selfnbyteser
   r
   r   
_osUrandom-   s   zRandomFactory._osUrandomFc                 C   sF   z|  |W S  ty   Y nw |rtjdtd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   fallbackr
   r
   r   secureRandom6   s   
zRandomFactory.secureRandomc                 C   s8   | j dur|  |d }d|d  | }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hexBytesr
   r
   r   	_randBitsS   s
   
zRandomFactory._randBits    c                    s   d  fddt|D S )z6
        Wrapper around the C{random} module.
        r%   c                    s   g | ]}t t jgqS 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   zRandomFactory._randModulec              	   C   s4   dD ]}z
t | ||W   S  ty   Y q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$   r0   N)getattrr   )r   r   srcr
   r
   r   r   f   s   
zRandomFactory.insecureRandomN)F)r   r   r   r	   randomSourcesr   r   r   r$   r&   	maketrans
_maketransr)   r0   r   r
   r
   r
   r   r      s    	
	

r   )r   r   r   )r	   r   r'   r   r1   r   r&   fromhexr!   RuntimeErrorr   r   r   factoryr   r   __all__r
   r
   r
   r   <module>   s   X