U
    P|j´  ã                   @   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[d
ddgZdS )zQ
Cryptographically secure random implementation, with fallback on normal random.
é    NÚgetrandbitsc                   @   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	   ú</tmp/pip-unpacked-wheel-audpp6lv/twisted/python/randbytes.pyr      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                   @   s|   e Zd ZdZdZeZeedœdd„Zdee	edœdd	„Z
eedœd
d„ZejZeddƒZeedœdd„Zeedœ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	   )ÚnbytesÚreturnc              
   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   )Úselfr   Úer	   r	   r
   Ú
_osUrandom-   s    zRandomFactory._osUrandomF)r   Úfallbackr   c                 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   r   r	   r	   r
   ÚsecureRandom6   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   Ú_fromhexr   )r   r   ÚnZ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   s,   z|   |¡W S  tk
r    Y nX |  |¡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/   r.   r	   r	   r
   r   f   s
    
zRandomFactory.insecureRandomN)F)r   r   r   r   ZrandomSourcesr   Úintr$   r   Úboolr   r"   Ú	maketransZ
_maketransr'   r/   r   r	   r	   r	   r
   r      s   		

r   r   r   )r   r   r%   r   Úgetattrr   r$   Úfromhexr    ÚRuntimeErrorr   r   r   Úfactoryr   r   Ú__all__r	   r	   r	   r
   Ú<module>   s   X