
    Ϫf                         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e	      Z
 G d de	      Z G d d	      Z e       Zej                  Zej                  Z[g d
Zy)zQ
Cryptographically secure random implementation, with fallback on normal random.
    Ngetrandbitsc                       e Zd ZdZy)SecureRandomNotAvailablezD
    Exception raised when no secure random algorithm is found.
    N__name__
__module____qualname____doc__     :/usr/lib/python3/dist-packages/twisted/python/randbytes.pyr   r          r   r   c                       e Zd ZdZy)SourceNotAvailablezQ
    Internal exception used when a specific random source is not available.
    Nr   r   r   r   r   r      r   r   r   c                       e Zd ZdZdZeZdedefdZddede	defdZ
dedefdZej                  Z ed	d	      Zdedefd
ZdedefdZy)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                 r    	 t        j                  |      S # t        t        f$ r}t	        |      d}~ww xY w)zO
        Wrapper around C{os.urandom} that cleanly manage its absence.
        N)osurandomAttributeErrorNotImplementedErrorr   )selfr   es      r   
_osUrandomzRandomFactory._osUrandom-   s7    	(::f%% 34 	($Q''	(s    616fallbackc                     	 | j                  |      S # t        $ r Y nw xY w|r-t        j                  dt        d       | j                  |      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   r   s      r   secureRandomzRandomFactory.secureRandom6   sd    	??6**! 		 MMM'	 &&v..*+NOO    	c                     | j                   *| j                  |dz        }d|dz  z  |z  }t        |      S t        d      )z3
        Wrapper around C{os.getrandbits}.
           z%%0%dxr   z#random.getrandbits is not available)r   _fromhexr   )r   r   nhexBytess       r   	_randBitszRandomFactory._randBitsS   sN     '  !,A FQJ/14HH%% !FGGr   r   c           
          dj                  t        |      D cg c]+  }t        t        j                  | j
                        g      - c}      S c c}w )z6
        Wrapper around the C{random} module.
        r   )joinrangebytesrandomchoice_BYTES)r   r   is      r   _randModulezRandomFactory._randModule`   s:     xxeFmTdkk :;<TUUTs   0Ac                 f    	 | j                  |      S # t        $ r Y nw xY w| j                  |      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   r6   )r   r   s     r   r%   zRandomFactory.insecureRandomf   s9    	>>&))! 		''r'   N)F)r   r   r	   r
   randomSourcesr   intr1   r   boolr&   r-   	maketrans
_maketransr4   r6   r%   r   r   r   r   r      s     MK( ( (P3 P$ P5 P:H H H JS!FV# V% V(S (U (r   r   )r&   r%   r   )r
   r   r2   r"   getattrr   r1   fromhexr*   RuntimeErrorr   r   r   factoryr&   r%   __all__r   r   r   <module>rB      s{   

 
  fmT2==|  U( U(p /##'' Ir   