
    wdf                        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        fD                       ZeddfdZeddfdZd Zd Z d Z!d Z"ddZ#d Z$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_native>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789iI z>Icompare_digestc              #   "   K   | ]
}|d v|V  dS ))N/N ).0seps     V/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/werkzeug/security.py	<genexpr>r       s5        C{4J4JC4J4J4J4J     c                 l    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rvs         r   
pbkdf2_hexr!   %   s3      
D$
FH	=	=BV]2{33444r   c                     |sd}t          |           } t          |          }t          |          r |            }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   
_test_hash	hash_names          r   r   r   9   ss    &  D>>DD>>D XZZ
J55			y$j&IIIr   c                    t          | t                    r|                     d          } t          |t                    r|                    d          }t          t          | |          S t	          |           t	          |          k    rdS d}t
          r9t          | |          D ]'\  }}|t          |          t          |          z  z  }(nt          | |          D ]\  }}|||z  z  }|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
    utf-8NFr   )
isinstancer	   r   _builtin_safe_str_cmplenr   r   ord)abr    xys        r   safe_str_cmpr5   Z   s     !Y HHW!Y HHW($Q***
1vvQu	
B
 AJJ 	" 	"DAq#a&&3q66/!BB	" AJJ 	 	DAq!a%KBB7Nr   c                     | dk    rt          d          d                    d t          |           D                       S )zAGenerate a random string of SALT_CHARS with specified ``length``.r   zSalt length must be positive c              3   T   K   | ]#}t                               t                    V  $d S N)_sys_rngchoice
SALT_CHARS)r   _s     r   r   zgen_salt.<locals>.<genexpr>|   s.      KK18??:..KKKKKKr   )
ValueErrorjoinr   )lengths    r   gen_saltrA   x   sC    {{788877KK
68J8JKKKKKKr   c                    | dk    r|| fS t          |t                    r|                    d          }|                     d          r|| dd                             d          }t          |          dvrt          d          |                    d	          } |rt          |d	         pd	          pt          }d
}d| |fz  }nd}| }|r%|st          d          t          ||||           }ny|rPt          |t                    r|                    d          }t          |||           }|                                }n't          j        | |                                          }||fS )zInternal password hash helper.  Supports plaintext without salt,
    unsalted and salted passwords.  In case salted passwords are used
    hmac is used.
    plainr,   zpbkdf2:   N:)r      z&Invalid number of arguments for PBKDF2r   Tzpbkdf2:%s:%dFzSalt is required for PBKDF2)r   )r-   r	   r   
startswithsplitr/   r>   popintDEFAULT_PBKDF2_ITERATIONSr!   _create_mac	hexdigestr'   new)	methodr   passwordargsr   	is_pbkdf2actual_methodr    macs	            r   _hash_internalrU      s   
 (I&& ,??7++## 
abbz$$t99F""EFFF!/c$q',Q//L3L
	&&*)==	 
7 	<:;;;$
VDDD	 7dI&& 	(;;w''D$&11]]__[**4466}r   c                     t                    rt          j        | |          S dfd	}||_        t          j        | ||          S )Nr   c                 .    t          j        |           S r9   )r'   rN   )drO   s    r   r   z_create_mac.<locals>.hashfunc   s    {61%%%r   )r   )r%   hmacHMAC__call__)keymsgrO   r   s     ` r   rL   rL      sc     +yc6***& & & & & &
 !H9S#x(((r   pbkdf2:sha256   c                 l    |dk    rt          |          nd}t          |||           \  }}|d|d|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.
    rC   r7   $)rA   rU   )rP   rO   salt_lengthr   hrS   s         r   generate_password_hashrd      sM    2 %+g$5$58K   2D%fdH==A}&aa00r   c                     |                      d          dk     rdS |                     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.
    ra   rF   Fr   )countrH   r5   rU   )pwhashrP   rO   r   hashvals        r   check_password_hashri      s[     ||C1u"LLa00FD'vtX>>qA7KKKr   c                 H   | g}|D ]dk    rt          j                  t          fdt          D                       s:t          j                                      sdk    s                    d          r dS |                               t          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``.
    r7   c              3       K   | ]}|v V  	d S r9   r   )r   r   filenames     r   r   zsafe_join.<locals>.<genexpr>   s'      88Cx888888r   z..z../N)
	posixpathnormpathany_os_alt_sepsospathisabsrG   appendr?   )	directory	pathnamespartsrl   s      @r   	safe_joinrx      s     KE  r>> )(33H 8888<88888	w}}X&&	 4""5))   44X>5!!r   )r^   r_   )&__doc__r   r'   rY   rq   rm   randomr   structr   _compatr   r   r   r	   r
   r   r<   rK   pack	_pack_intr&   r.   r:   listrr   r   altseprp   r!   r   r5   rA   rU   rL   rd   ri   rx   r   r   r   <module>r      s       				                                                    M
" F4LL	&6== <>>t  GK0     5TD5 5 5 5* 5TDJ J J JB  <L L L" " "J
) 
) 
)1 1 1 1<L L L"" " " " "r   