U
    
W[w                     @   s   d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	 e
eddZe	rXejZndd ZG dd	 d	eZG d
d deZG dd deZe ZejZejZ[ddd	gZdS )zQ
Cryptographically secure random implementation, with fallback on normal random.
    )divisionabsolute_importN)_PY3getrandbitsc                 C   s
   |  dS )Nhex)decode)hexBytes r	   :/usr/lib/python3/dist-packages/twisted/python/randbytes.py_fromhex   s    r   c                   @   s   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	   r	   r
   r      s   r   c                   @   s   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      s   r   c                   @   sf   e Zd ZdZdZeZdd ZdddZdd	 Ze	rBe
jZd
d ZnejZdd Ze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   s@   zt |W S  ttfk
r: } zt|W 5 d}~X Y nX dS )zO
        Wrapper around C{os.urandom} that cleanly manage its absence.
        N)osurandomAttributeErrorNotImplementedErrorr   )selfnbyteser	   r	   r
   
_osUrandom5   s    zRandomFactory._osUrandomFc                 C   sL   z|  |W S  tk
r    Y nX |r@tjdtdd | |S tddS )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 availableN)r   r   warningswarnRuntimeWarninginsecureRandomr   )r   r   Zfallbackr	   r	   r
   secureRandom?   s    
zRandomFactory.secureRandomc                 C   s<   | j dk	r0|  |d }d|d  | }t|S tddS )z3
        Wrapper around C{os.getrandbits}.
        N   z%%0%dxr   z#random.getrandbits is not available)r   r   r   )r   r   nr   r	   r	   r
   	_randBits\   s
    
zRandomFactory._randBitsc                    s   d  fddt|D S )>
            Wrapper around the C{random} module.
                c                    s   g | ]}t t jgqS r	   )bytesrandomchoice_BYTES.0ir   r	   r
   
<listcomp>m   s    -RandomFactory._randModule.<locals>.<listcomp>joinranger   r   r	   r0   r
   _randModulei   s    zRandomFactory._randModulec                    s   d  fddt|D S )r'   r(   c                    s   g | ]}t  jqS r	   )r*   r+   r,   r-   r0   r	   r
   r1   u   s    r2   r3   r6   r	   r0   r
   r7   q   s    r(   c              	   C   s8   dD ].}zt | ||W   S  tk
r0   Y qX q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   srcr	   r	   r
   r"   {   s
    
zRandomFactory.insecureRandomN)F)r   r   r   r   ZrandomSourcesr   r   r#   r&   r   r)   	maketransZ
_maketransr7   stringr,   r"   r	   r	   r	   r
   r   &   s   	



r   r#   r"   )r   Z
__future__r   r   r   r   r*   r;   Ztwisted.python.compatr   r8   r   r)   fromhexr   RuntimeErrorr   r   objectr   factoryr#   r"   __all__r	   r	   r	   r
   <module>   s    g