ó
³Ebc           @   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 „  e j j e j j g Dƒ ƒ Z e d d d „ Z e d d d „ Z d „  Z  d „  Z! d „  Z" d „  Z# d d d „ Z$ d „  Z% d „  Z& d S(   s«   
    werkzeug.security
    ~~~~~~~~~~~~~~~~~

    Security related helpers such as secure password hashing tools.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
iÿÿÿÿN(   t   SystemRandom(   t   Structi   (   t   izip(   t   PY2(   t
   range_type(   t	   text_type(   t   to_bytes(   t	   to_nativet>   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789iðI s   >It   compare_digestc         c   s!   |  ] } | d k r | Vq d S(   t   /N(   NR
   (   t   None(   t   .0t   sep(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pys	   <genexpr>!   s    c         C   s.   t  |  | | | | ƒ } t t j | d ƒ ƒ S(   s)  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.
    t	   hex_codec(   t
   pbkdf2_binR   t   codecst   encode(   t   datat   saltt
   iterationst   keylent   hashfunct   rv(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt
   pbkdf2_hex%   s    c         C   sp   | s d } n  t  |  ƒ }  t  | ƒ } t | ƒ rQ | ƒ  } t | d d ƒ } n | } t j | |  | | | ƒ S(   sò  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.
    t   sha256t   nameN(   R   t   callablet   getattrR   t   hashlibt   pbkdf2_hmac(   R   R   R   R   R   t
   _test_hasht	   hash_name(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyR   9   s    		c         C   sø   t  |  t ƒ r! |  j d ƒ }  n  t  | t ƒ rB | j d ƒ } n  t d k	 r[ t |  | ƒ St |  ƒ t | ƒ k rw t Sd } t rÀ xh t |  | ƒ D]& \ } } | t	 | ƒ t	 | ƒ AO} q“ Wn. x+ t |  | ƒ D] \ } } | | | AO} qÐ W| d k S(   sö   This 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
    s   utf-8i    N(
   t
   isinstanceR   R   t   _builtin_safe_str_cmpR   t   lent   FalseR   R   t   ord(   t   at   bR   t   xt   y(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   safe_str_cmpZ   s    !c         C   s8   |  d k r t  d ƒ ‚ n  d j d „  t |  ƒ Dƒ ƒ S(   sA   Generate a random string of SALT_CHARS with specified ``length``.i    s   Salt length must be positivet    c         s   s   |  ] } t  j t ƒ Vq d  S(   N(   t   _sys_rngt   choicet
   SALT_CHARS(   R   t   _(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pys	   <genexpr>|   s    (   t
   ValueErrort   joinR   (   t   length(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   gen_saltx   s    c   	      C   sp  |  d k r | |  f St  | t ƒ r7 | j d ƒ } n  |  j d ƒ rÄ |  d j d ƒ } t | ƒ d k rz t d ƒ ‚ n  | j d	 ƒ }  | r¥ t | d	 pŸ d	 ƒ p¨ t	 } t
 } d
 |  | f } n t } |  } | r| së t d ƒ ‚ n  t | | | d |  ƒ} n` | rNt  | t ƒ r-| j d ƒ } n  t | | |  ƒ } | j ƒ  } n t j |  | ƒ j ƒ  } | | f S(   s   Internal password hash helper.  Supports plaintext without salt,
    unsalted and salted passwords.  In case salted passwords are used
    hmac is used.
    t   plains   utf-8s   pbkdf2:i   t   :i   i   s&   Invalid number of arguments for PBKDF2i    s   pbkdf2:%s:%ds   Salt is required for PBKDF2R   (   i   i   (   R!   R   R   t
   startswitht   splitR#   R0   t   popt   intt   DEFAULT_PBKDF2_ITERATIONSt   TrueR$   R   t   _create_mact	   hexdigestR   t   new(	   t   methodR   t   passwordt   argsR   t	   is_pbkdf2t   actual_methodR   t   mac(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   _hash_internal   s2    
"c            sM   t  ˆ  ƒ r t j |  | ˆ  ƒ Sd ‡  f d † } | | _ t j |  | | ƒ S(   NR+   c            s   t  j ˆ  |  ƒ S(   N(   R   R>   (   t   d(   R?   (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyR   ¨   s    (   R   t   hmact   HMACt   __call__(   t   keyt   msgR?   R   (    (   R?   si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyR<   ¤   s
    	s   pbkdf2:sha256i   c         C   sG   | d k r t  | ƒ n d } t | | |  ƒ \ } } d | | | f S(   sø  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.
    R4   R+   s   %s$%s$%s(   R3   RE   (   R@   R?   t   salt_lengthR   t   hRC   (    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   generate_password_hash±   s    c         C   sQ   |  j  d ƒ d k  r t S|  j d d ƒ \ } } } t t | | | ƒ d | ƒ S(   sÉ  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.
    t   $i   i    (   t   countR$   R7   R*   RE   (   t   pwhashR@   R?   R   t   hashval(    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   check_password_hashÏ   s    c            sŸ   |  g } x† | D]~ ‰  ˆ  d k r4 t  j ˆ  ƒ ‰  n  t ‡  f d †  t Dƒ ƒ s} t j j ˆ  ƒ s} ˆ  d k s} ˆ  j d ƒ r d S| j	 ˆ  ƒ q Wt  j
 | Œ  S(   s2  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(   N(    (   R   R   (   t   filename(    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pys	   <genexpr>ð   s    s   ..s   ../N(   t	   posixpatht   normpatht   anyt   _os_alt_sepst   ost   patht   isabsR6   R   t   appendR1   (   t	   directoryt	   pathnamest   parts(    (   RT   si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt	   safe_joinà   s    		('   t   __doc__R   R   RG   RY   RU   t   randomR    t   structR   t   _compatR   R   R   R   R   R   R.   R:   t   packt	   _pack_intR   R   R"   R,   t   listRZ   R   t   altsepRX   R   R   R*   R3   RE   R<   RN   RS   R`   (    (    (    si   /var/www/html/facial-emotion-detection-webapp-main/flask/lib/python2.7/site-packages/werkzeug/security.pyt   <module>
   s:   	% 			%		