o
    ٭h                     @   s2   d dl Z ddlmZ ddlmZ G dd dZdS )    N   )
exceptions)HasherProtocolc                
   @   s   e Zd ZdZdeje ddfddZedddZ	dd	d
ej
eef dej
edf defddZd
ej
eef dej
eef defddZd
ej
eef dej
eef dejeej
edf f fddZdS )PasswordHashz0
    Represents a password hashing utility.
    hashersreturnNc                 C   s(   t |dks
J d|| _|d | _dS )z
        Args:
            hashers: A sequence of hashers to be used for password hashing.

        Raises:
            AssertionError: If no hashers are specified.
        r   z%You must specify at least one hasher.N)lenr   current_hasher)selfr    r   Q/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/pwdlib/_hash.py__init__   s   zPasswordHash.__init__c                 C   s   ddl m} | | fS )ag  
        Returns a PasswordHash instance with recommended hashers.

        Currently, the hasher is Argon2 with default parameters.

        Examples:
            >>> password_hash = PasswordHash.recommended()
            >>> hash = password_hash.hash("herminetincture")
            >>> password_hash.verify(hash, "herminetincture")
            True
        r   )Argon2Hasher)hashers.argon2r   )clsr   r   r   r   recommended   s   zPasswordHash.recommendedsaltpasswordr   c                C   s   | j j||dS )aE  
        Hashes a password using the current hasher.

        Args:
            password: The password to be hashed.
            salt: The salt to be used for hashing. Defaults to None.

        Returns:
            The hashed password.

        Examples:
            >>> hash = password_hash.hash("herminetincture")
        r   )r	   hash)r
   r   r   r   r   r   r   )   s   zPasswordHash.hashr   c                 C   s0   | j D ]}||r|||  S qt|)a#  
        Verifies if a password matches a given hash.

        Args:
            password: The password to be checked.
            hash: The hash to be verified.

        Returns:
            True if the password matches the hash, False otherwise.

        Raises:
            exceptions.UnknownHashError: If the hash is not recognized by any of the hashers.

        Examples:
            >>> password_hash.verify("herminetincture", hash)
            True

            >>> password_hash.verify("INVALID_PASSWORD", hash)
            False
        )r   identifyverifyr   UnknownHashError)r
   r   r   hasherr   r   r   r   >   s
   


zPasswordHash.verifyc                 C   sb   | j D ](}||r+|||s dS d}|| jks||r%| j|}d|f  S qt|)aw  
        Verifies if a password matches a given hash and updates the hash if necessary.

        Args:
            password: The password to be checked.
            hash: The hash to be verified.

        Returns:
            A tuple containing a boolean indicating if the password matches the hash,
                and an updated hash if the current hasher or the hash itself needs to be updated.

        Raises:
            exceptions.UnknownHashError: If the hash is not recognized by any of the hashers.

        Examples:
            >>> valid, updated_hash = password_hash.verify_and_update("herminetincture", hash)
        )FNNT)r   r   r   r	   check_needs_rehashr   r   r   )r
   r   r   r   updated_hashr   r   r   verify_and_updateZ   s   


zPasswordHash.verify_and_update)r   r   )__name__
__module____qualname____doc__typingSequencer   r   classmethodr   Unionstrbytesr   boolr   Tupler   r   r   r   r   r      s6    

r   )r!    r   r   r   r   r   r   r   r   <module>   s    