3
¥YðiÞ  ã            	   @   sŒ  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZejr@dZ	dZ
edd„ ejjejjgD ƒƒZeje e
ddfejeef ejeef eeje ejejeejf  edœdd„Ze
ddfejeef ejeef eeje ejejeejf  edœd	d
„Zeeedœdd„Zeedœdd„Zeeeejeef dœdd„Zdeeeedœdd„Zeeedœdd„Zeeeje dœdd„Z dS ) é    NZ>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789i ÷ c             c   s"   | ]}|d k	r|dkr|V  qd S )Nú/© )Ú.0Úsepr   r   ú5/tmp/pip-build-8om4l6ph/Werkzeug/werkzeug/security.pyú	<genexpr>   s    r   )ÚdataÚsaltÚ
iterationsÚkeylenÚhashfuncÚreturnc             C   s$   t jdtdd� t| ||||ƒjƒ S )aœ  Like :func:`pbkdf2_bin`, but returns a hex-encoded string.

    :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.

    .. deprecated:: 2.0
        Will be removed in Werkzeug 2.1. Use :func:`hashlib.pbkdf2_hmac`
        instead.

    .. versionadded:: 0.9
    zj'pbkdf2_hex' is deprecated and will be removed in Werkzeug 2.1. Use 'hashlib.pbkdf2_hmac().hex()' instead.é   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningÚ
pbkdf2_binÚhex)r   r	   r
   r   r   r   r   r   Ú
pbkdf2_hex   s
    r   c             C   sj   t jdtdd� t| tƒr$| jdƒ} t|tƒr8|jdƒ}|sBd}nt|ƒrT|ƒ j}n|}tj	|| |||ƒS )ae  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.

    :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.

    .. deprecated:: 2.0
        Will be removed in Werkzeug 2.1. Use :func:`hashlib.pbkdf2_hmac`
        instead.

    .. versionadded:: 0.9
    zd'pbkdf2_bin' is deprecated and will be removed in Werkzeug 2.1. Use 'hashlib.pbkdf2_hmac()' instead.r   )r   Úutf8Úsha256)
r   r   r   Ú
isinstanceÚstrÚencodeÚcallableÚnameÚhashlibÚpbkdf2_hmac)r   r	   r
   r   r   Ú	hash_namer   r   r   r   5   s    




r   )ÚaÚbr   c             C   sD   t jdtdd� t| tƒr$| jdƒ} t|tƒr8|jdƒ}tj| |ƒS )ai  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.

    .. deprecated:: 2.0
        Will be removed in Werkzeug 2.1. Use
        :func:`hmac.compare_digest` instead.

    .. versionadded:: 0.7
    zd'safe_str_cmp' is deprecated and will be removed in Werkzeug 2.1. Use 'hmac.compare_digest' instead.r   )r   zutf-8)r   r   r   r   r   r   ÚhmacÚcompare_digest)r    r!   r   r   r   Úsafe_str_cmpg   s    



r$   )Úlengthr   c             C   s(   | 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)ÚsecretsÚchoiceÚ
SALT_CHARS)r   Ú_r   r   r   r   ˆ   s    zgen_salt.<locals>.<genexpr>)Ú
ValueErrorÚjoinÚrange)r%   r   r   r   Úgen_saltƒ   s    r.   )Úmethodr	   Úpasswordr   c             C   sÔ   | dkr|| fS |j dƒ}|j dƒ}| jdƒr¦|s:tdƒ‚| dd… jdƒ}t|ƒdkr`td
ƒ‚|jdƒ} |r~t|d pzdƒnt}tj	| |||ƒj
ƒ d| › d|› �fS |rÀtj||| ƒjƒ | fS tj| |ƒjƒ | fS )z�Internal password hash helper.  Supports plaintext without salt,
    unsalted and salted passwords.  In case salted passwords are used
    hmac is used.
    Úplainzutf-8zpbkdf2:zSalt is required for PBKDF2é   Nú:é   r   z&Invalid number of arguments for PBKDF2r   )r4   r   )r   Ú
startswithr+   ÚsplitÚlenÚpopÚintÚDEFAULT_PBKDF2_ITERATIONSr   r   r   r"   ÚnewÚ	hexdigest)r/   r	   r0   Úargsr
   r   r   r   Ú_hash_internal‹   s"    



r>   úpbkdf2:sha256é   )r0   r/   Úsalt_lengthr   c             C   s8   |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.
    r1   r&   ú$)r.   r>   )r0   r/   rA   r	   ÚhZactual_methodr   r   r   Úgenerate_password_hash¬   s    rD   )Úpwhashr0   r   c             C   s<   | j dƒdk rdS | jddƒ\}}}tj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.
    rB   r   Fr   )Úcountr6   r"   r#   r>   )rE   r0   r/   r	   Zhashvalr   r   r   Úcheck_password_hashÌ   s    rG   )Ú	directoryÚ	pathnamesr   c                sr   | g}x`|D ]X‰ ˆ dkr"t jˆ ƒ‰ t‡ fdd„tD ƒƒsVtjjˆ ƒsVˆ dksVˆ jdƒrZdS |jˆ ƒ qW 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``.
    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Úisabsr5   Úappendr,   )rH   rI   Úpartsr   )rJ   r   Ú	safe_joinÞ   s    	


rT   )r?   r@   )!r   r"   rO   rK   r'   ÚtypingÚtr   ZTYPE_CHECKINGr)   r:   ÚlistrP   r   ÚaltseprN   ÚListr   ÚUnionÚbytesr9   ÚOptionalÚCallabler   r   Úboolr$   r.   ÚTupler>   rD   rG   rT   r   r   r   r   Ú<module>   s6    &::,"