ó
î;,Uc           @   sÿ   d  Z  d d l m Z d d l m Z d d l m Z m	 Z
 m Z m Z d e f d „  ƒ  YZ e e e ƒ Z e ƒ  Z e Z d „  Z d „  Z d	 „  Z d
 „  Z e d „ Z d „  Z e d „ Z d „  Z d „  Z e e
 d d ƒ d k rñ [ n  e
 j  ƒ  d S(   sz   
PRNG management routines, thin wrappers.

See the file RATIONALE for a short explanation of why this module was written.
iÿÿÿÿ(   t   partial(   t   integer_types(   t   ffit   libt   exception_from_error_queuet   path_stringt   Errorc           B   s   e  Z d  Z RS(   s5   
    An error occurred in an `OpenSSL.rand` API.
    (   t   __name__t
   __module__t   __doc__(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyR      s   c         C   s   t  |  t ƒ s t d ƒ ‚ n  |  d k  r9 t d ƒ ‚ n  t j d |  ƒ } t j | |  ƒ } | d k rs t ƒ  n  t j	 | ƒ S(   s†   
    Get some random bytes as a string.

    :param num_bytes: The number of bytes to fetch
    :return: A string of random bytes
    s   num_bytes must be an integeri    s   num_bytes must not be negatives   char[]iÿÿÿÿ(
   t
   isinstancet   _integer_typest	   TypeErrort
   ValueErrort   _ffit   newt   _libt
   RAND_bytest   _raise_current_errort   buffer(   t	   num_bytest   result_buffert   result_code(    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   bytes   s    
c         C   sY   t  |  t ƒ s t d ƒ ‚ n  t  | t ƒ s< t d ƒ ‚ n  t j |  t |  ƒ | ƒ d S(   s¶   
    Add data with a given entropy to the PRNG

    :param buffer: Buffer with random data
    :param entropy: The entropy (in bytes) measurement of the buffer
    :return: None
    s   buffer must be a byte strings   entropy must be an integerN(   R
   t   _builtin_bytesR   t   intR   t   RAND_addt   len(   R   t   entropy(    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   add5   s
    c         C   s8   t  |  t ƒ s t d ƒ ‚ n  t j |  t |  ƒ ƒ d S(   sx   
    Alias for rand_add, with entropy equal to length

    :param buffer: Buffer with random data
    :return: None
    s   buffer must be a byte stringN(   R
   R   R   R   t	   RAND_seedR   (   R   (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   seedH   s    c           C   s
   t  j ƒ  S(   sj   
    Retrieve the status of the PRNG

    :return: True if the PRNG is seeded enough, false otherwise
    (   R   t   RAND_status(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   statusW   s    c         C   sa   t  |  t ƒ s t d ƒ ‚ n  | t k r3 d } n t  | t ƒ sQ t d ƒ ‚ n  t j |  | ƒ S(   s¥  
    Query an entropy gathering daemon (EGD) for random data and add it to the
    PRNG. I haven't found any problems when the socket is missing, the function
    just returns 0.

    :param path: The path to the EGD socket
    :param bytes: (optional) The number of bytes to read, default is 255
    :returns: The number of bytes read (NB: a value of 0 isn't necessarily an
              error, check rand.status())
    s   path must be a byte stringiÿ   s   bytes must be an integer(   R
   R   R   t   _unspecifiedR   R   t   RAND_egd_bytes(   t   pathR   (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   egda   s    	c           C   s   t  j ƒ  d S(   s?   
    Erase the memory used by the PRNG.

    :return: None
    N(   R   t   RAND_cleanup(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   cleanupx   s    c         C   sO   t  |  ƒ }  | t k r! d } n t | t ƒ s? t d ƒ ‚ n  t j |  | ƒ S(   s	  
    Seed the PRNG with data from a file

    :param filename: The file to read data from (``bytes`` or ``unicode``).
    :param maxbytes: (optional) The number of bytes to read, default is to read
        the entire file

    :return: The number of bytes read
    iÿÿÿÿs   maxbytes must be an integer(   t   _path_stringR"   R
   R   R   R   t   RAND_load_file(   t   filenamet   maxbytes(    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt	   load_fileƒ   s    
	c         C   s   t  |  ƒ }  t j |  ƒ S(   s™   
    Save PRNG state to a file

    :param filename: The file to write data to (``bytes`` or ``unicode``).

    :return: The number of bytes written
    (   R(   R   t   RAND_write_file(   R*   (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt
   write_file˜   s    c           C   s   t  j ƒ  d S(   sq   
    Add the current contents of the screen to the PRNG state. Availability:
    Windows.

    :return: None
    N(   R   t   RAND_screen(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   screen¥   s    R/   N(!   R	   t	   functoolsR    t   sixR   R   t   OpenSSL._utilR   R   R   R   R   t   _exception_from_error_queueR   R(   t	   ExceptionR   R   t   objectR"   R   R   R   R   R!   R%   R'   R,   R.   R0   t   getattrt   Nonet   ERR_load_RAND_strings(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   <module>   s&   "					
				