ó
írUc           @   sW   d  Z  d d l Z d d l m Z m Z d d l m Z d   Z d   Z d   Z	 d S(   s(   Functions for generating random numbers.i˙˙˙˙N(   t   commont	   transform(   t   bytec         C   sj   t  |  d  \ } } t j |  } | d k rf t t j d   } | d | L} t |  | } n  | S(   s   Reads 'nbits' random bits.

    If nbits isn't a whole number of bytes, an extra byte will be appended with
    only the lower bits set.
    i   i    i   (   t   divmodt   ost   urandomt   ordR   (   t   nbitst   nbytest   rbitst
   randomdatat   randomvalue(    (    s,   c:\Python27\lib\site-packages\rsa\randnum.pyt   read_random_bits   s    c         C   s1   t  |   } t j |  } | d |  d >O} | S(   s8   Reads a random integer of approximately nbits bits.
    i   (   R   R   t	   bytes2int(   R   R
   t   value(    (    s,   c:\Python27\lib\site-packages\rsa\randnum.pyt   read_random_int/   s    c         C   so   t  j |   } d } xS t rj t |  } | |  k r: Pn  | r] | d d k r] | d 8} n  | d 7} q W| S(   sâ   Returns a random integer x with 1 <= x <= maxvalue
    
    May take a very long time in specific situations. If maxvalue needs N bits
    to store, the closer maxvalue is to (2 ** N) - 1, the faster this function
    is.
    i    i
   i   (   R    t   bit_sizet   TrueR   (   t   maxvalueR   t   triesR   (    (    s,   c:\Python27\lib\site-packages\rsa\randnum.pyt   randint<   s    	(
   t   __doc__R   t   rsaR    R   t   rsa._compatR   R   R   R   (    (    (    s,   c:\Python27\lib\site-packages\rsa\randnum.pyt   <module>   s   		