ó
[³XMc           @   s¯   d  Z  d d l Z d d l Z d d l Z e e d d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e
 f d	 „  ƒ  YZ e ƒ  Z e j Z e j Z [ d
 d d g Z d S(   sQ   
Cryptographically secure random implementation, with fallback on normal random.
iÿÿÿÿNt   getrandbitst   SecureRandomNotAvailablec           B   s   e  Z d  Z RS(   sD   
    Exception raised when no secure random algorithm is found.
    (   t   __name__t
   __module__t   __doc__(    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyR      s   t   SourceNotAvailablec           B   s   e  Z d  Z RS(   sQ   
    Internal exception used when a specific random source is not available.
    (   R   R   R   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyR      s   t   RandomFactoryc           B   sS   e  Z d  Z d Z e Z d „  Z d „  Z e d „ Z d „  Z	 d „  Z
 d „  Z RS(	   sy  
    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.

    @cvar randomSources: list of file sources used when os.urandom is not
        available.
    @type randomSources: C{tuple}
    s   /dev/urandomc         C   s=   y t  j | ƒ SWn% t t f k
 r8 } t | ƒ ‚ n Xd S(   sO   
        Wrapper around C{os.urandom} that cleanly manage its absence.
        N(   t   ost   urandomt   AttributeErrort   NotImplementedErrorR   (   t   selft   nbytest   e(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt
   _osUrandom-   s    c         C   sx   x[ |  j  D]P } y t | d ƒ } Wn t t f k
 r< q
 X| j | ƒ } | j ƒ  | Sq
 Wt d |  j  f ƒ ‚ d S(   sœ   
        Wrapper around random file sources.

        This method isn't meant to be call out of the class and could be
        removed arbitrarily.
        t   rbs   File sources not available: %sN(   t   randomSourcest   filet   IOErrort   OSErrort   readt   closeR   (   R   R   t   srct   ft   bytes(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt   _fileUrandom7   s    
c         C   sx   x9 d	 D]1 } y t  |  | ƒ | ƒ SWq t k
 r7 q Xq W| rh t j d d t d d ƒ|  j | ƒ St d ƒ ‚ d S(
   sk  
        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}
        R   R   sP   urandom unavailable - proceeding with non-cryptographically secure random sourcet   categoryt
   stackleveli   s!   No secure random source availableN(   s
   _osUrandoms   _fileUrandom(   t   getattrR   t   warningst   warnt   RuntimeWarningt   insecureRandomR   (   R   R   t   fallbackR   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt   secureRandomK   s    c         C   sQ   |  j  d k	 rA |  j  | d ƒ } d | d | } | j d ƒ St d ƒ ‚ d S(   s3   
        Wrapper around C{os.getrandbits}.
        i   s   %%0%dxi   t   hexs#   random.getrandbits is not availableN(   R    t   Nonet   decodeR   (   R   R   t   nt   hexBytes(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt	   _randBitsh   s
    c         C   s=   d } x0 t  | ƒ D]" } | t t j d d ƒ ƒ 7} q W| S(   s5   
        Wrapper around C{random.randrange}.
        t    i    iÿ   (   t   xranget   chrt   randomt	   randrange(   R   R   R   t   i(    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt
   _randRanges   s     c         C   s@   x9 d D]1 } y t  |  | ƒ | ƒ SWq t k
 r7 q Xq Wd S(   sÒ   
        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/   N(   s	   _randBitss
   _randRange(   R   R   (   R   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyR    }   s
    
(   s   /dev/urandom(   R   R   R   R   R    R   R   t   FalseR"   R(   R/   R    (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyR      s   	
			
R"   R    (   R   R   R   R,   R   R$   R    t   RuntimeErrorR   R   t   objectR   t   factoryR"   R    t   __all__(    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/python/randbytes.pyt   <module>   s   $r			