o
    Óó·i´  ã                   @   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
   ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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                   @   sˆ   e Zd ZdZdZeZdedefdd„Zddede	defd	d
„Z
dedefdd„ZejZeddƒZdedefdd„Zdedef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   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   )Úselfr   Úer
   r
   r   Ú
_osUrandom-   s   €ÿzRandomFactory._osUrandomFÚfallbackc                 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 availableN)r   r   ÚwarningsÚwarnÚRuntimeWarningÚinsecureRandomr   )r   r   r   r
   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   Ú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>N)ÚjoinÚrange©r   r   r
   r,   r   Ú_randModule`   s   zRandomFactory._randModulec                 C   s*   z|   |¡W S  ty   Y nw |  |¡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}
        N)r$   r   r1   r0   r
   r
   r   r   f   s   
ÿ
zRandomFactory.insecureRandomN)F)r   r   r   r	   ZrandomSourcesr   Úintr&   r   Úboolr    r$   Ú	maketransZ
_maketransr)   r1   r   r
   r
   r
   r   r      s    		

r   )r    r   r   )r	   r   r'   r   Úgetattrr   r&   Úfromhexr"   ÚRuntimeErrorr   r   r   Úfactoryr    r   Ú__all__r
   r
   r
   r   Ú<module>   s   X