ķ
ž^Yc           @   s`   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   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(   /tmp/pip-build-kpPAdC/rsa/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(   /tmp/pip-build-kpPAdC/rsa/rsa/randnum.pyt   read_random_int0   s    c         C   s   t  |   } | d BS(   sh   Reads a random odd integer of approximately nbits bits.

    >>> read_random_odd_int(512) & 1
    1
    i   (   R   (   R   R   (    (    s(   /tmp/pip-build-kpPAdC/rsa/rsa/randnum.pyt   read_random_odd_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(   /tmp/pip-build-kpPAdC/rsa/rsa/randnum.pyt   randintK   s    	(   t   __doc__R   t   rsaR    R   t   rsa._compatR   R   R   R   R   (    (    (    s(   /tmp/pip-build-kpPAdC/rsa/rsa/randnum.pyt   <module>   s   			