o
    Mf6a                     @   s  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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mZmZmZ d dlmZ d dlmZ d	Zd
Zdd Zd8ddZd8ddZe dd Ze dd Z eedd Z!d9ddZ"dd Z#d:ddZ$d d! Z%G d"d# d#Z&G d$d% d%e&Z'G d&d' d'e'Z(G d(d) d)e&Z)G d*d+ d+e&Z*G d,d- d-e*Z+G d.d/ d/e&Z,G d0d1 d1e&Z-G d2d3 d3e&Z.G d4d5 d5e&Z/G d6d7 d7e&Z0dS );    N)settings)ImproperlyConfigured)setting_changed)receiver)RANDOM_STRING_CHARSconstant_time_compareget_random_stringpbkdf2)import_string)gettext_noop!(   c                 C   s   | du p	|  t S )zv
    Return True if this password wasn't generated by
    User.set_unusable_password(), i.e. make_password(None).
    N)
startswithUNUSABLE_PASSWORD_PREFIX)encoded r   ]/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/contrib/auth/hashers.pyis_password_usable   s   r   defaultc                 C   s   | du st |s
dS t|}zt|}W n
 ty   Y dS w |j|jk}|p+||}|| |}|s>|s>|r>|| | |rH|rH|rH||  |S )z
    Return a boolean of whether the raw password matches the three
    part encoded digest.

    If setter is specified, it'll be called when you need to
    regenerate the password.
    NF)r   
get_hasheridentify_hasher
ValueError	algorithmmust_updateverifyharden_runtime)passwordr   setter	preferredhasherhasher_changedr   
is_correctr   r   r   check_password   s    r"   c                 C   sT   | du r
t tt S t| ttfstdt| j t	|}|p#|
 }|| |S )a|  
    Turn a plain-text password into a hash for database storage

    Same as encode() but generate a new random salt. If password is None then
    return a concatenation of UNUSABLE_PASSWORD_PREFIX and a random string,
    which disallows logins. Additional random string reduces chances of gaining
    access to staff or superuser accounts. See ticket #20079 for more info.
    Nz+Password must be a string or bytes, got %s.)r   r   UNUSABLE_PASSWORD_SUFFIX_LENGTH
isinstancebytesstr	TypeErrortype__qualname__r   saltencode)r   r*   r   r   r   r   make_passwordA   s   	r,   c                  C   sB   g } t jD ]}t|}| }t|dstd| | | q| S )Nr   z,hasher doesn't specify an algorithm name: %s)r   PASSWORD_HASHERSr
   getattrr   append)hashershasher_path
hasher_clsr   r   r   r   get_hashersV   s   

r3   c                   C   s   dd t  D S )Nc                 S   s   i | ]}|j |qS r   r   ).0r   r   r   r   
<dictcomp>e   s    z,get_hashers_by_algorithm.<locals>.<dictcomp>)r3   r   r   r   r   get_hashers_by_algorithmc   s   r7   c                  K   s$   | d dkrt   t  d S d S )Nsettingr-   )r3   cache_clearr7   )kwargsr   r   r   reset_hashersh   s   r;   c                 C   sL   t | dr| S | dkrt d S t }z||  W S  ty%   td|  w )z
    Return an instance of a loaded password hasher.

    If algorithm is 'default', return the default hasher. Lazily import hashers
    specified in the project's settings file if needed.
    r   r   r   z\Unknown password hashing algorithm '%s'. Did you specify it in the PASSWORD_HASHERS setting?)hasattrr3   r7   KeyErrorr   )r   r0   r   r   r   r   o   s   


r   c                 C   sp   t | dkr
d| vst | dkr| drd}t|S t | dkr,| dr,d}t|S | dd	d
 }t|S )z
    Return an instance of a loaded password hasher.

    Identify hasher algorithm by examining encoded hash, and call
    get_hasher() to return hasher. Raise ValueError if
    algorithm cannot be identified, or if hasher is not loaded.
        $%   md5$$unsalted_md5.   sha1$$unsalted_sha1   r   )lenr   splitr   )r   r   r   r   r   r      s   
r      *c                 C   s(   | d| }||t | |d  7 }|S )z
    Return the given hash, with only the first ``show`` number shown. The
    rest are masked with ``char`` for security reasons.
    N)rG   )hashshowcharmaskedr   r   r   	mask_hash   s   rO   c                 C   s   t | tt t |k S N)rG   mathlog2r   )r*   expected_entropyr   r   r   must_update_salt   s   rT   c                   @   s\   e Zd ZdZdZdZdZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )BasePasswordHasherz
    Abstract base class for password hashers

    When creating your own hasher, you need to override algorithm,
    verify(), encode() and safe_summary().

    PasswordHasher objects are immutable.
    N   c              
   C   sz   | j d ur5t| j ttfr| j \}}n| j }zt|}W |S  ty4 } z
td| jj	|f d }~ww td| jj	 )Nz&Couldn't load %r algorithm library: %sz-Hasher %r doesn't specify a library attribute)
libraryr$   tuplelist	importlibimport_moduleImportErrorr   	__class____name__)selfnamemod_pathmoduleer   r   r   _load_library   s    

z BasePasswordHasher._load_libraryc                 C   s&   t | jt tt }t|tdS )z
        Generate a cryptographically secure nonce salt in ASCII with an entropy
        of at least `salt_entropy` bits.
        )allowed_chars)rQ   ceilsalt_entropyrR   rG   r   r   )r_   
char_countr   r   r   r*      s   zBasePasswordHasher.saltc                 C      t d)z'Check if the given password is correct.z?subclasses of BasePasswordHasher must provide a verify() methodNotImplementedErrorr_   r   r   r   r   r   r      s   zBasePasswordHasher.verifyc                 C   ri   )z
        Create an encoded database value.

        The result is normally formatted as "algorithm$salt$hash" and
        must be fewer than 128 characters.
        z@subclasses of BasePasswordHasher must provide an encode() methodrj   r_   r   r*   r   r   r   r+         zBasePasswordHasher.encodec                 C   ri   )z
        Return a decoded database value.

        The result is a dictionary and should contain `algorithm`, `hash`, and
        `salt`. Extra keys can be algorithm specific like `iterations` or
        `work_factor`.
        z@subclasses of BasePasswordHasher must provide a decode() method.rj   r_   r   r   r   r   decode   s   zBasePasswordHasher.decodec                 C   ri   )z
        Return a summary of safe values.

        The result is a dictionary and will be used where the password field
        must be displayed to construct a safe representation of the password.
        zEsubclasses of BasePasswordHasher must provide a safe_summary() methodrj   ro   r   r   r   safe_summary   rn   zBasePasswordHasher.safe_summaryc                 C      dS )NFr   ro   r   r   r   r         zBasePasswordHasher.must_updatec                 C   s   t d dS )a  
        Bridge the runtime gap between the work factor supplied in `encoded`
        and the work factor suggested by this hasher.

        Taking PBKDF2 as an example, if `encoded` contains 20000 iterations and
        `self.iterations` is 30000, this method should run password through
        another 10000 iterations of PBKDF2. Similar approaches should exist
        for any hasher that has a work factor. If not, this method should be
        defined as a no-op to silence the warning.
        zIsubclasses of BasePasswordHasher should provide a harden_runtime() methodN)warningswarnrl   r   r   r   r      s   z!BasePasswordHasher.harden_runtime)r^   
__module__r)   __doc__r   rW   rg   rd   r*   r   r+   rp   rq   r   r   r   r   r   r   rU      s    
		rU   c                   @   sP   e Zd ZdZdZdZejZdddZ	dd Z
d	d
 Zdd Zdd Zdd ZdS )PBKDF2PasswordHashera  
    Secure password hashing using the PBKDF2 algorithm (recommended)

    Configured to use PBKDF2 + HMAC + SHA256.
    The result is a 64 byte binary string.  Iterations may be changed
    safely but you must rename the algorithm if you change SHA256.
    pbkdf2_sha256i Nc                 C   s^   |d usJ |rd|vsJ |p| j }t|||| jd}t|d }d| j|||f S )Nr?   )digestasciiz%s$%d$%s$%s)
iterationsr	   rz   base64	b64encoderp   stripr   )r_   r   r*   r|   rK   r   r   r   r+     s   
zPBKDF2PasswordHasher.encodec                 C   s4   | dd\}}}}|| jksJ ||t||dS )Nr?      )r   rK   r|   r*   rH   r   int)r_   r   r   r|   r*   rK   r   r   r   rp     s   zPBKDF2PasswordHasher.decodec                 C   s*   |  |}| ||d |d }t||S Nr*   r|   rp   r+   r   r_   r   r   decoded	encoded_2r   r   r   r   !  s   

zPBKDF2PasswordHasher.verifyc              
   C   sF   |  |}td|d td|d tdt|d tdt|d iS )Nr   r|   r*   rK   rp   _rO   r_   r   r   r   r   r   rq   &     
z!PBKDF2PasswordHasher.safe_summaryc                 C   s,   |  |}t|d | j}|d | jkp|S r   )rp   rT   rg   r|   )r_   r   r   update_saltr   r   r   r   /  s   
z PBKDF2PasswordHasher.must_updatec                 C   s:   |  |}| j|d  }|dkr| ||d | d S d S )Nr|   r   r*   )rp   r|   r+   )r_   r   r   r   extra_iterationsr   r   r   r   4  s
   
z#PBKDF2PasswordHasher.harden_runtimerP   )r^   rv   r)   rw   r   r|   hashlibsha256rz   r+   rp   r   rq   r   r   r   r   r   r   rx     s    

	rx   c                   @   s   e Zd ZdZdZejZdS )PBKDF2SHA1PasswordHasherz
    Alternate PBKDF2 hasher which uses SHA1, the default PRF
    recommended by PKCS #5. This is compatible with other
    implementations of PBKDF2, such as openssl's
    PKCS5_PBKDF2_HMAC_SHA1().
    pbkdf2_sha1N)r^   rv   r)   rw   r   r   sha1rz   r   r   r   r   r   ;  s    
r   c                   @   s\   e Zd ZdZdZdZdZdZdZdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd ZdS )Argon2PasswordHashera  
    Secure password hashing using the argon2 algorithm.

    This is the winner of the Password Hashing Competition 2013-2015
    (https://password-hashing.net). It requires the argon2-cffi library which
    depends on native C code and might cause portability issues.
    argon2   i     c              	   C   sL   |   }|  }|jj| | |j|j|j|j|j	d}| j
|d S )N)	time_costmemory_costparallelismhash_lenr(   r{   )rd   params	low_levelhash_secretr+   r   r   r   r   r(   r   rp   )r_   r   r*   r   r   datar   r   r   r+   U  s   	zArgon2PasswordHasher.encodec              
   C   s   |   }|dd\}}|| jksJ |d| }|d^}}}}	|dt| d  7 }t|d}
||	|j|j	|
|j
||j|d	S )Nr?   rF   =   latin1)	r   rK   r   r   r*   r   varietyversionr   )rd   rH   r   extract_parametersrG   r}   	b64decoderp   r   r   r   r   )r_   r   r   r   restr   r   r   b64saltrK   r*   r   r   r   rp   c  s"   zArgon2PasswordHasher.decodec                 C   sV   |   }|dd\}}|| jksJ z| d| |W S  |jjy*   Y dS w )Nr?   rF   F)rd   rH   r   PasswordHasherr   
exceptionsVerificationError)r_   r   r   r   r   r   r   r   r   r   x  s   zArgon2PasswordHasher.verifyc                 C   sv   |  |}td|d td|d td|d td|d td|d td|d td	t|d	 td
t|d
 iS )Nr   r   r   zmemory costr   z	time costr   r   r*   rK   r   r   r   r   r   rq     s   
z!Argon2PasswordHasher.safe_summaryc                 C   s>   |  |}|d }|  }|j|_t|d | j}||kp|S )Nr   r*   )rp   r   salt_lenrT   rg   )r_   r   r   current_params
new_paramsr   r   r   r   r     s   
z Argon2PasswordHasher.must_updatec                 C      d S rP   r   rl   r   r   r   r     s   z#Argon2PasswordHasher.harden_runtimec              	   C   s4   |   }|j|jjj|jj|j|j| j| j	| j
dS )N)r(   r   r   r   r   r   r   )rd   
Parametersr   TypeIDARGON2_VERSIONDEFAULT_RANDOM_SALT_LENGTHDEFAULT_HASH_LENGTHr   r   r   )r_   r   r   r   r   r     s   zArgon2PasswordHasher.paramsN)r^   rv   r)   rw   r   rW   r   r   r   r+   rp   r   rq   r   r   r   r   r   r   r   r   F  s    	
r   c                   @   sZ   e Zd ZdZdZejZdZdZ	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd ZdS )BCryptSHA256PasswordHashera&  
    Secure password hashing using the bcrypt algorithm (recommended)

    This is considered by many to be the most secure algorithm but you
    must first install the bcrypt library.  Please be warned that
    this library depends on native C code and might cause portability
    issues.
    bcrypt_sha256)bcryptr      c                 C   s   |   }|| jS rP   )rd   gensaltrounds)r_   r   r   r   r   r*     s   zBCryptSHA256PasswordHasher.saltc                 C   sN   |   }| }| jd urt| | }|||}d| j|df S )Nz%s$%sr{   )rd   r+   rz   binasciihexlifyhashpwr   rp   )r_   r   r*   r   r   r   r   r   r+     s   
z!BCryptSHA256PasswordHasher.encodec                 C   sH   | dd\}}}}}|| jksJ |||dd  |d d t|dS )Nr?   r      )r   algostrchecksumr*   work_factorr   )r_   r   r   emptyr   r   r   r   r   r   rp     s   

z!BCryptSHA256PasswordHasher.decodec                 C   s:   | dd\}}|| jksJ | ||d}t||S )Nr?   rF   r{   )rH   r   r+   r   )r_   r   r   r   r   r   r   r   r   r     s   
z!BCryptSHA256PasswordHasher.verifyc              
   C   sF   |  |}td|d td|d tdt|d tdt|d iS )Nr   zwork factorr   r*   r   r   r   r   r   r   rq     r   z'BCryptSHA256PasswordHasher.safe_summaryc                 C   s   |  |}|d | jkS )Nr   )rp   r   r   r   r   r   r     s   
z&BCryptSHA256PasswordHasher.must_updatec                 C   sr   | dd\}}|d d }| dd }d| jt|  d }|dkr7| ||d |d8 }|dks$d S d S )Nr?   rF      r   r   r{   )rH   r   r   r+   )r_   r   r   r   r   r*   r   diffr   r   r   r     s   z)BCryptSHA256PasswordHasher.harden_runtimeN)r^   rv   r)   rw   r   r   r   rz   rW   r   r*   r+   rp   r   rq   r   r   r   r   r   r   r     s    	r   c                   @   s   e Zd ZdZdZdZdS )BCryptPasswordHashera  
    Secure password hashing using the bcrypt algorithm

    This is considered by many to be the most secure algorithm but you
    must first install the bcrypt library.  Please be warned that
    this library depends on native C code and might cause portability
    issues.

    This hasher does not first hash the password which means it is subject to
    bcrypt's 72 bytes password truncation. Most use cases should prefer the
    BCryptSHA256PasswordHasher.
    r   N)r^   rv   r)   rw   r   rz   r   r   r   r   r     s    r   c                   @   D   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )SHA1PasswordHasherz?
    The SHA1 password hashing algorithm (not recommended)
    r   c                 C   B   |d usJ |rd|vsJ t ||   }d| j||f S Nr?   %s$%s$%s)r   r   r+   	hexdigestr   r_   r   r*   rK   r   r   r   r+   	     zSHA1PasswordHasher.encodec                 C   ,   | dd\}}}|| jksJ |||dS Nr?   r   r   rK   r*   rH   r   r_   r   r   r*   rK   r   r   r   rp        zSHA1PasswordHasher.decodec                 C   $   |  |}| ||d }t||S Nr*   r   r   r   r   r   r        

zSHA1PasswordHasher.verifyc                 C   >   |  |}td|d tdt|d ddtdt|d iS Nr   r*   r   rL   rK   r   r   r   r   r   rq     
   
zSHA1PasswordHasher.safe_summaryc                 C      |  |}t|d | jS r   rp   rT   rg   r   r   r   r   r   %     
zSHA1PasswordHasher.must_updatec                 C   r   rP   r   rl   r   r   r   r   )  rs   z!SHA1PasswordHasher.harden_runtimeNr^   rv   r)   rw   r   r+   rp   r   rq   r   r   r   r   r   r   r         	r   c                   @   r   )MD5PasswordHasherzE
    The Salted MD5 password hashing algorithm (not recommended)
    md5c                 C   r   r   )r   r   r+   r   r   r   r   r   r   r+   3  r   zMD5PasswordHasher.encodec                 C   r   r   r   r   r   r   r   rp   9  r   zMD5PasswordHasher.decodec                 C   r   r   r   r   r   r   r   r   B  r   zMD5PasswordHasher.verifyc                 C   r   r   r   r   r   r   r   rq   G  r   zMD5PasswordHasher.safe_summaryc                 C   r   r   r   r   r   r   r   r   O  r   zMD5PasswordHasher.must_updatec                 C   r   rP   r   rl   r   r   r   r   S  rs   z MD5PasswordHasher.harden_runtimeNr   r   r   r   r   r   -  r   r   c                   @   r   )UnsaltedSHA1PasswordHashera7  
    Very insecure algorithm that you should *never* use; store SHA1 hashes
    with an empty salt.

    This class is implemented because Django used to accept such password
    hashes. Some older Django installs still have these values lingering
    around so we need to handle and upgrade them properly.
    rE   c                 C   rr   N r   r_   r   r   r   r*   b  rs   zUnsaltedSHA1PasswordHasher.saltc                 C   s&   |dksJ t |  }d| S )Nr   zsha1$$%s)r   r   r+   r   r   r   r   r   r+   e  s   z!UnsaltedSHA1PasswordHasher.encodec                 C   s$   | dsJ | j|dd  d dS )NrD   rI   r   )r   r   ro   r   r   r   rp   j  s
   
z!UnsaltedSHA1PasswordHasher.decodec                 C   s   |  |d}t||S r   )r+   r   r_   r   r   r   r   r   r   r   r  s   
z!UnsaltedSHA1PasswordHasher.verifyc                 C   s*   |  |}td|d tdt|d iS )Nr   rK   r   r   r   r   r   rq   v  s   
z'UnsaltedSHA1PasswordHasher.safe_summaryc                 C   r   rP   r   rl   r   r   r   r   }  rs   z)UnsaltedSHA1PasswordHasher.harden_runtimeNr^   rv   r)   rw   r   r*   r+   rp   r   rq   r   r   r   r   r   r   W  s    r   c                   @   r   )UnsaltedMD5PasswordHashera  
    Incredibly insecure algorithm that you should *never* use; stores unsalted
    MD5 hashes without the algorithm prefix, also accepts MD5 hashes with an
    empty salt.

    This class is implemented because Django used to store passwords this way
    and to accept such password hashes. Some older Django installs still have
    these values lingering around so we need to handle and upgrade them
    properly.
    rB   c                 C   rr   r   r   r   r   r   r   r*     rs   zUnsaltedMD5PasswordHasher.saltc                 C   s   |dksJ t |  S r   )r   r   r+   r   rm   r   r   r   r+     s   z UnsaltedMD5PasswordHasher.encodec                 C   s   | j |d dS )Nr   r4   ro   r   r   r   rp     s   z UnsaltedMD5PasswordHasher.decodec                 C   s8   t |dkr|dr|dd  }| |d}t||S )Nr@   rA      r   )rG   r   r+   r   r   r   r   r   r     s   
z UnsaltedMD5PasswordHasher.verifyc                 C   s.   |  |}td|d tdt|d ddiS )Nr   rK   r   r   r   r   r   r   r   rq     s   
z&UnsaltedMD5PasswordHasher.safe_summaryc                 C   r   rP   r   rl   r   r   r   r     rs   z(UnsaltedMD5PasswordHasher.harden_runtimeNr   r   r   r   r   r     s    
r   c                   @   sH   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	dd Z
dd ZdS )CryptPasswordHasherzv
    Password hashing using UNIX crypt (not recommended)

    The crypt module is not supported on all platforms.
    cryptc                 C   s   t dS )Nr   )r   r   r   r   r   r*     s   zCryptPasswordHasher.saltc                 C   s@   |   }t|dksJ |||}|d usJ d| jd|f S )Nr   r   r   )rd   rG   r   r   )r_   r   r*   r   rK   r   r   r   r+     s
   zCryptPasswordHasher.encodec                 C   r   r   r   r   r   r   r   rp     r   zCryptPasswordHasher.decodec                 C   s0   |   }| |}|||d }t|d |S )NrK   )rd   rp   r   r   )r_   r   r   r   r   r   r   r   r   r     s   
zCryptPasswordHasher.verifyc              	   C   s:   |  |}td|d td|d tdt|d ddiS )Nr   r*   rK   r   r   r   r   r   r   r   rq     s
   
z CryptPasswordHasher.safe_summaryc                 C   r   rP   r   rl   r   r   r   r     rs   z"CryptPasswordHasher.harden_runtimeN)r^   rv   r)   rw   r   rW   r*   r+   rp   r   rq   r   r   r   r   r   r     s    	r   )Nr   )r   )rI   rJ   )1r}   r   	functoolsr   rZ   rQ   rt   django.confr   django.core.exceptionsr   django.core.signalsr   django.dispatchr   django.utils.cryptor   r   r   r	   django.utils.module_loadingr
   django.utils.translationr   r   r   r#   r   r"   r,   	lru_cacher3   r7   r;   r   r   rO   rT   rU   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sP    

"





Y8eG***,