ó
*§ÑVc           @   s  d  Z  d d l Z d d l 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(   s*   
PRNG management routines, thin wrappers.
iÿÿÿÿN(   t   partial(   t   integer_types(   t   ffit   libt   exception_from_error_queuet   path_stringt   Errorc           B   s   e  Z d  Z RS(   s  
    An error occurred in an :mod:`OpenSSL.rand` API.

    If the current RAND method supports any errors, this is raised when needed.
    The default method does not raise this when the entropy pool is depleted.

    Whenever this exception is raised directly, it has a list of error messages
    from the OpenSSL error queue, where each item is a tuple *(lib, function,
    reason)*. Here *lib*, *function* and *reason* are all strings, describing
    where and what the problem is.

    See :manpage:`err(3)` for more information.
    (   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 from the PRNG as a string.

    This is a wrapper for the C function ``RAND_bytes``.

    :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|  
    Mix bytes from *string* into the PRNG state.

    The *entropy* argument is (the lower bound of) an estimate of how much
    randomness is contained in *string*, measured in bytes.

    For more information, see e.g. :rfc:`1750`.

    :param buffer: Buffer with random data.
    :param entropy: The entropy (in bytes) measurement of the buffer.

    :return: :obj:`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   addC   s
    c         C   s8   t  |  t ƒ s t d ƒ ‚ n  t j |  t |  ƒ ƒ d S(   s›   
    Equivalent to calling :func:`add` with *entropy* as the length of *buffer*.

    :param buffer: Buffer with random data

    :return: :obj:`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   seed[   s    c           C   s
   t  j ƒ  S(   s’   
    Check whether the PRNG has been seeded with enough data.

    :return: :obj:`True` if the PRNG is seeded enough, :obj:`False` otherwise.
    (   R   t   RAND_status(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   statusj   s    c         C   sx   t  j d t ƒ t |  t ƒ s. t d ƒ ‚ n  | t k rC d } n t | t ƒ sa t d ƒ ‚ n  t t	 j
 | ƒ ƒ | S(   s   
    Query the system random source and seed the PRNG.

    Does *not* actually query the EGD.

    .. deprecated:: 16.0.0
        EGD was only necessary for some commercial UNIX systems that all
        reached their ends of life more than a decade ago.  See
        `pyca/cryptography#1636
        <https://github.com/pyca/cryptography/pull/1636>`_.

    :param path: Ignored.
    :param bytes: (optional) The number of bytes to read, default is 255.

    :returns: ``len(bytes)`` or 255 if not specified.
    s.   OpenSSL.rand.egd() is deprecated as of 16.0.0.s   path must be a byte stringiÿ   s   bytes must be an integer(   t   warningst   warnt   DeprecationWarningR
   R   R   t   _unspecifiedR   R   t   ost   urandom(   t   pathR   (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   egds   s    		c           C   s   t  j ƒ  d S(   s‚   
    Erase the memory used by the PRNG.

    This is a wrapper for the C function ``RAND_cleanup``.

    :return: :obj:`None`
    N(   R   t   RAND_cleanup(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   cleanup“   s    	c         C   sO   t  |  ƒ }  | t k r! d } n t | t ƒ s? t d ƒ ‚ n  t j |  | ƒ S(   sq  
    Read *maxbytes* of data from *filename* and seed the PRNG with it.

    Read the whole file if *maxbytes* is not specified or negative.

    :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  
    Write a number of random bytes (currently 1024) to the file *path*.  This
    file can then be used with :func:`load_file` to seed the PRNG again.

    :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(   sr   
    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    R3   (#   R	   R&   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+   R0   R2   R4   t   getattrt   Nonet   ERR_load_RAND_strings(    (    (    s0   /usr/lib/python2.7/dist-packages/OpenSSL/rand.pyt   <module>   s*   "						 			
