
_                 @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l
 m Z d d l
 m Z d d	 l
 m Z d d
 l
 m Z d d l
 m Z d Z d Z e	 d  j Z e e d d  Z e   Z e d d   e j j e j j g D  Z e d d d d  Z e d d d d  Z d d   Z d d   Z  d d   Z! d d   Z" d d d  d!  Z# d" d#   Z$ d$ d%   Z% d S)&z
    werkzeug.security
    ~~~~~~~~~~~~~~~~~

    Security related helpers such as secure password hashing tools.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N)SystemRandom)Struct   )izip)PY2)
range_type)	text_type)to_bytes)	to_nativeZ>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789iI z>Icompare_digestc             c   s!   |  ] } | d k r | Vq d  S)N/)Nr    ).0sepr   r   5/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/security.py	<genexpr>!   s    r   c             C   s.   t  |  | | | |  } t t j | d   S)a)  Like :func:`pbkdf2_bin`, but returns a hex-encoded string.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided,
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function, or a function
                     from the hashlib module.  Defaults to sha256.
    	hex_codec)
pbkdf2_binr
   codecsencode)datasalt
iterationskeylenhashfuncrvr   r   r   
pbkdf2_hex%   s    r   c             C   sm   | s d } t  |   }  t  |  } t |  rN |   } t | d d  } n | } t j | |  | | |  S)a  Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-256 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha256.
    sha256nameN)r	   callablegetattrhashlibpbkdf2_hmac)r   r   r   r   r   Z
_test_hash	hash_namer   r   r   r   9   s    	r   c             C   s   t  |  t  r |  j d  }  t  | t  r< | j d  } t d k	 rU t |  |  St |   t |  k rq d Sd } t r xh t |  |  D]& \ } } | t |  t |  AO} q Wn. x+ t |  |  D] \ } } | | | AO} q W| d k S)zThis function compares strings in somewhat constant time.  This
    requires that the length of at least one string is known in advance.

    Returns `True` if the two strings are equal, or `False` if they are not.

    .. versionadded:: 0.7
    zutf-8NFr   )
isinstancer   r   _builtin_safe_str_cmplenr   r   ord)abr   xyr   r   r   safe_str_cmpZ   s    !r,   c             C   s8   |  d k r t  d   d j d d   t |   D  S)zAGenerate a random string of SALT_CHARS with specified ``length``.r   zSalt length must be positive c             s   s   |  ] } t  j t  Vq d  S)N)_sys_rngchoice
SALT_CHARS)r   _r   r   r   r   |   s    zgen_salt.<locals>.<genexpr>)
ValueErrorjoinr   )lengthr   r   r   gen_saltx   s    r5   c       	      C   sj  |  d k r | |  f St  | t  r4 | j d  } |  j d  r |  d d  j d  } t |  d k rz t d	   | j d
  }  | r t | d
 p d
  p t	 } d } d |  | f } n d } |  } | r| s t d   t
 | | | d |  } n] | rHt  | t  r'| j d  } t | | |   } | j   } n t j |  |  j   } | | f S)zInternal password hash helper.  Supports plaintext without salt,
    unsalted and salted passwords.  In case salted passwords are used
    hmac is used.
    plainzutf-8zpbkdf2:   N:r      z&Invalid number of arguments for PBKDF2r   Tzpbkdf2:%s:%dFzSalt is required for PBKDF2r   )r   r9   )r$   r   r   
startswithsplitr&   r2   popintDEFAULT_PBKDF2_ITERATIONSr   _create_mac	hexdigestr!   new)	methodr   passwordargsr   Z	is_pbkdf2actual_methodr   macr   r   r   _hash_internal   s2    
"rG   c                sP   t     r t j |  |    Sd   f d d  } | | _ t j |  | |  S)N    c                s   t  j   |   S)N)r!   rA   )d)rB   r   r   r      s    z_create_mac.<locals>.hashfunc)r   hmacHMAC__call__)keymsgrB   r   r   )rB   r   r?      s
    	r?   zpbkdf2:sha256   c             C   sG   | d k r t  |  n d } t | | |   \ } } d | | | f S)a  Hash a password with the given method and salt with a string of
    the given length. The format of the string returned includes the method
    that was used so that :func:`check_password_hash` can check the hash.

    The format for the hashed string looks like this::

        method$salt$hash

    This method can **not** generate unsalted passwords but it is possible
    to set param method='plain' in order to enforce plaintext passwords.
    If a salt is used, hmac is used internally to salt the password.

    If PBKDF2 is wanted it can be enabled by setting the method to
    ``pbkdf2:method:iterations`` where iterations is optional::

        pbkdf2:sha256:80000$salt$hash
        pbkdf2:sha256$salt$hash

    :param password: the password to hash.
    :param method: the hash method to use (one that hashlib supports). Can
                   optionally be in the format ``pbkdf2:<method>[:iterations]``
                   to enable PBKDF2.
    :param salt_length: the length of the salt in letters.
    r6   r-   z%s$%s$%s)r5   rG   )rC   rB   Zsalt_lengthr   hrE   r   r   r   generate_password_hash   s    rQ   c             C   sQ   |  j  d  d k  r d S|  j d d  \ } } } t t | | |  d |  S)a  check a password against a given salted and hashed password value.
    In order to support unsalted legacy passwords this method supports
    plain text passwords, md5 and sha1 hashes (both salted and unsalted).

    Returns `True` if the password matched, `False` otherwise.

    :param pwhash: a hashed string like returned by
                   :func:`generate_password_hash`.
    :param password: the plaintext password to compare against the hash.
    $r9   Fr   )countr;   r,   rG   )ZpwhashrC   rB   r   Zhashvalr   r   r   check_password_hash   s    rT   c                s   |  g } x | D]~     d k r1 t  j      t   f d d   t D  s} t j j    s}   d k s}   j d  r d S| j    q Wt  j	 |   S)a2  Safely join zero or more untrusted path components to a base
    directory to avoid escaping the base directory.

    :param directory: The trusted base directory.
    :param pathnames: The untrusted path components relative to the
        base directory.
    :return: A safe path, otherwise ``None``.
    r-   c             3   s   |  ] } |   k Vq d  S)Nr   )r   r   )filenamer   r   r      s    zsafe_join.<locals>.<genexpr>z..z../N)
	posixpathnormpathany_os_alt_sepsospathisabsr:   appendr3   )	directoryZ	pathnamespartsr   )rU   r   	safe_join   s    		r`   )&__doc__r   r!   rJ   rZ   rV   randomr   structr   _compatr   r   r   r   r	   r
   r0   r>   packZ	_pack_intr    r%   r.   listr[   r   altseprY   r   r   r,   r5   rG   r?   rQ   rT   r`   r   r   r   r   <module>
   s:   	( %