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