
    k"g$[                        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 Zd'dZd(dZd(dZd(dZej@                  d        Z!ej@                  d        Z" ee      d        Z#d'dZ$d Z%d)dZ&d Z' G d d      Z( G d de(      Z) G d de)      Z* G d de(      Z+ G d d e(      Z, G d! d"e,      Z- G d# d$e(      Z. G d% d&e(      Z/y)*    N)settings)ImproperlyConfigured)setting_changed)receiver)RANDOM_STRING_CHARSconstant_time_compareget_random_stringpbkdf2)import_string)gettext_noop!(   c                 :    | du xs | j                  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)encodeds    T/var/www/html/djangosite/lib/python3.12/site-packages/django/contrib/auth/hashers.pyis_password_usabler      s"    
 d?N'"4"45M"NNN    c                 d   | du xs t        |       }t        |      }	 t        |      }|rt	        t        t                     yj                  |j                  k7  }|xs |j                  |      }|j                  | |      }|s|s|r|j                  | |       ||fS # t        $ r d}Y w xY w)z
    Return two booleans. The first is whether the raw password matches the
    three part encoded digest, and the second whether to regenerate the
    password.
    NT)FF)r   
get_hasheridentify_hasher
ValueErrormake_passwordr	   UNUSABLE_PASSWORD_SUFFIX_LENGTH	algorithmmust_updateverifyharden_runtime)passwordr   	preferredfake_runtimehasherhasher_changedr   
is_corrects           r   verify_passwordr&   $   s     t#F+=g+F'FL9%I )
  	'(GHI%%)<)<<N BI$9$9'$BKx1J nh0{""-  s   B! !B/.B/c                 D    t        | ||      \  }}|r|r
|r ||        |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.
    r!   r&   r    r   setterr!   r%   r   s         r   check_passwordr,   H   s,     .h9UJ*xr   c                 `   K   t        | ||      \  }}|r|r|r ||        d{    |S 7 w)zSee check_password().r(   Nr)   r*   s         r   acheck_passwordr.   V   s9     -h9UJ*X 	s   #.,.c                    | t         t        t              z   S t        | t        t
        f      s!t        dt        |       j                  z        t        |      }|xs |j                         }|j                  | |      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.
    z+Password must be a string or bytes, got %s.)r   r	   r   
isinstancebytesstr	TypeErrortype__qualname__r   saltencode)r    r6   r#   s      r   r   r   ^   s~     '*;++
 
 	
 h-9DN<W<WW
 	
 F 6;;=D==4((r   c                      g } t         j                  D ]?  }t        |      } |       }t        |d      st	        d|z        | j                  |       A | S )Nr   z,hasher doesn't specify an algorithm name: %s)r   PASSWORD_HASHERSr   getattrr   append)hashershasher_path
hasher_clsr#   s       r   get_hashersr?   t   sa    G00 ";/
v{+&>L  	v Nr   c                  R    t               D  ci c]  } | j                  |  c} S c c} w N)r?   r   )r#   s    r   get_hashers_by_algorithmrB      s#    3>=AFf$AAAs   $c                 `    | dk(  r)t         j                          t        j                          y y )Nr9   )r?   cache_clearrB   )settingkwargss     r   reset_hashersrG      s(    $$! ,,. %r   c                     t        | d      r| S | dk(  rt               d   S t               }	 ||    S # t        $ r t	        d| z        w xY 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   defaultr   z\Unknown password hashing algorithm '%s'. Did you specify it in the PASSWORD_HASHERS setting?)hasattrr?   rB   KeyErrorr   )r   r<   s     r   r   r      sl     y+&	i	}Q +,	9%% 	&' 	s	   1 A	c                    t        |       dk(  rd| vst        |       dk(  r| j                  d      rd}t        |      S t        |       dk(  r| j                  d      rd}t        |      S | j                  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.
        $%   zmd5$$unsalted_md5.   zsha1$$unsalted_sha1   r   )lenr   splitr   )r   r   s     r   r   r      s     	Gs'1Gw11':"	 i  	 
W	 2 28 <#	 i   MM#q)!,	i  r   c                 8    | d| }||t        | |d       z  z  }|S )z
    Return the given hash, with only the first ``show`` number shown. The
    rest are masked with ``char`` for security reasons.
    N)rT   )hashshowcharmaskeds       r   	mask_hashr[      s.    
 %4[F
dSde%%%FMr   c                 d    t        |       t        j                  t        t                    z  |k  S rA   )rT   mathlog2r   )r6   expected_entropys     r   must_update_saltr`      s&    t9tyy%8!9::=MMMr   c                   R    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zy)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                 r   | j                   Tt        | j                   t        t        f      r| j                   \  }}n| j                   }	 t	        j
                  |      }|S t        d| j                  j                  z        # t        $ r*}t        d| j                  j                  d|      d }~ww xY w)NzCouldn't load z algorithm library: z-Hasher %r doesn't specify a library attribute)
libraryr0   tuplelist	importlibimport_moduleImportErrorr   	__class____name__)selfnamemod_pathmodulees        r   _load_libraryz BasePasswordHasher._load_library   s    <<#$,,6!%h<<"00: M;dnn>U>UU
 	
   ~~..3 s   
B 	B6%B11B6c                     t        j                  | j                  t        j                  t	        t
                    z        }t        |t
              S )z
        Generate a cryptographically secure nonce salt in ASCII with an entropy
        of at least `salt_entropy` bits.
        )allowed_chars)r]   ceilsalt_entropyr^   rT   r   r	   )rm   
char_counts     r   r6   zBasePasswordHasher.salt   s:     YYt00499SAT=U3VVW
 ;NOOr   c                     t        d      )z'Check if the given password is correct.z?subclasses of BasePasswordHasher must provide a verify() methodNotImplementedErrorrm   r    r   s      r   r   zBasePasswordHasher.verify   s    !M
 	
r   c                 @    |t        d      |rd|v rt        d      y )Nzpassword must be provided.rN   z+salt must be provided and cannot contain $.)r3   r   rm   r    r6   s      r   _check_encode_argsz%BasePasswordHasher._check_encode_args   s.    899sd{JKK #r   c                     t        d      )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() methodry   r}   s      r   r7   zBasePasswordHasher.encode  s     "N
 	
r   c                     t        d      )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.ry   rm   r   s     r   decodezBasePasswordHasher.decode  s     "N
 	
r   c                     t        d      )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() methodry   r   s     r   safe_summaryzBasePasswordHasher.safe_summary  s     "S
 	
r   c                      y)NF r   s     r   r   zBasePasswordHasher.must_update$  s    r   c                 .    t        j                  d       y)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warnr{   s      r   r   z!BasePasswordHasher.harden_runtime'  s     	W	
r   )rl   
__module__r5   __doc__r   re   rv   rr   r6   r   r~   r7   r   r   r   r   r   r   r   rb   rb      sG     IGL
$P
L	


	

r   rb   c                   V    e Zd ZdZdZdZej                  ZddZ	d Z
d Zd Zd	 Zd
 Zy)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_sha256ipF Nc                    | j                  ||       |xs | j                  }t        |||| j                        }t	        j
                  |      j                  d      j                         }d| j                  |||fz  S )N)digestasciiz%s$%d$%s$%s)	r~   
iterationsr
   r   base64	b64encoder   stripr   )rm   r    r6   r   rW   s        r   r7   zPBKDF2PasswordHasher.encodeD  sp    $/24??
hjE%,,W5;;=
D$GGGr   c                 r    |j                  dd      \  }}}}|| j                  k(  sJ ||t        |      |dS )NrN      )r   rW   r   r6   rU   r   int)rm   r   r   r   r6   rW   s         r   r   zPBKDF2PasswordHasher.decodeK  sG    ,3MM#q,A)	:tTDNN***"j/	
 	
r   c                 n    | j                  |      }| j                  ||d   |d         }t        ||      S Nr6   r   r   r7   r   rm   r    r   decoded	encoded_2s        r   r   zPBKDF2PasswordHasher.verifyU  s8    ++g&KK'&/7<;PQ	$Wi88r   c                     | j                  |      }t        d      |d   t        d      |d   t        d      t        |d         t        d      t        |d         iS )Nr   r   r6   rW   r   _r[   rm   r   r   s      r   r   z!PBKDF2PasswordHasher.safe_summaryZ  sZ    ++g&kNGK0lOW\2fIy1fIy1	
 	
r   c                     | j                  |      }t        |d   | j                        }|d   | j                  k7  xs |S r   )r   r`   rv   r   )rm   r   r   update_salts       r   r   z PBKDF2PasswordHasher.must_updatec  s?    ++g&&wv8I8IJ%8H[Hr   c                     | j                  |      }| j                  |d   z
  }|dkD  r| j                  ||d   |       y y )Nr   r   r6   )r   r   r7   )rm   r    r   r   extra_iterationss        r   r   z#PBKDF2PasswordHasher.harden_runtimeh  sF    ++g&??W\-BBaKK'&/3CD  r   rA   )rl   r   r5   r   r   r   hashlibsha256r   r7   r   r   r   r   r   r   r   r   r   r   7  s=      IJ^^FH
9

I
Er   r   c                   ,    e Zd ZdZdZej                  Zy)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)rl   r   r5   r   r   r   sha1r   r   r   r   r   r   o  s     I\\Fr   r   c                   N    e Zd ZdZdZdZdZdZdZd Z	d Z
d Zd	 Zd
 Zd Zd Zy)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           	      ^   | j                         }| j                         }|j                  j                  |j	                         |j	                         |j
                  |j                  |j                  |j                  |j                        }| j                  |j                  d      z   S )N)	time_costmemory_costparallelismhash_lenr4   r   )rr   params	low_levelhash_secretr7   r   r   r   r   r4   r   r   )rm   r    r6   r   r   datas         r   r7   zArgon2PasswordHasher.encode  s    ##%++OOKKM&&****__ , 
 ~~G 444r   c           
         | j                         }|j                  dd      \  }}|| j                  k(  sJ |j                  d|z         }|j                  d      ^}}}}	|dt	        |       dz  z  z  }t        j                  |      j                  d      }
||	|j                  |j                  |
|j                  ||j                  |d	S )NrN   rS   =   latin1)	r   rW   r   r   r6   r   varietyversionr   )rr   rU   r   extract_parametersrT   r   	b64decoder   r   r   r   r   )rm   r   r   r   restr   r   r   b64saltrW   r6   s              r   r   zArgon2PasswordHasher.decode  s    ##%!--Q/	4DNN*****3:6%)ZZ_"!Wd33w<-!+,,(//9"!--!--))~~

 
	
r   c                     | j                         }|j                  dd      \  }}|| j                  k(  sJ 	 |j                         j	                  d|z   |      S # |j
                  j                  $ r Y yw xY w)NrN   rS   F)rr   rU   r   PasswordHasherr   
exceptionsVerificationError)rm   r    r   r   r   r   s         r   r   zArgon2PasswordHasher.verify  sy    ##%!--Q/	4DNN***	((*11#*hGG  22 		s   "A A76A7c                 ,   | j                  |      }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   r6   rW   r   r   s      r   r   z!Argon2PasswordHasher.safe_summary  s    ++g&kNGK0iL'),iL'),mgm4kNGK0mgm4fIy1fIy1	
 		
r   c                     | j                  |      }|d   }| j                         }|j                  |_        t        |d   | j                        }||k7  xs |S )Nr   r6   )r   r   salt_lenr`   rv   )rm   r   r   current_params
new_paramsr   s         r   r   z Argon2PasswordHasher.must_update  s[    ++g& *[[]
 -55
&wv8I8IJ*,<<r   c                      y rA   r   r{   s      r   r   z#Argon2PasswordHasher.harden_runtime       	r   c           	         | j                         }|j                  |j                  j                  j                  |j                  j
                  |j                  |j                  | j                  | j                  | j                        S )N)r4   r   r   r   r   r   r   )rr   
Parametersr   TypeIDARGON2_VERSIONDEFAULT_RANDOM_SALT_LENGTHDEFAULT_HASH_LENGTHr   r   r   )rm   r   s     r   r   zArgon2PasswordHasher.params  sw    ##%  !!&&))$$3366//nn(((( ! 
 	
r   N)rl   r   r5   r   r   re   r   r   r   r7   r   r   r   r   r   r   r   r   r   r   r   {  sE     IGIKK5
*
=

r   r   c                   ^    e Zd ZdZdZej                  ZdZdZ	d Z
d Zd Zd Zd	 Zd
 Zd Zy)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                 X    | j                         }|j                  | j                        S rA   )rr   gensaltrounds)rm   r   s     r   r6   zBCryptSHA256PasswordHasher.salt  s#    ##%~~dkk**r   c                 "   | j                         }|j                         }| j                  2t        j                  | j                  |      j                               }|j                  ||      }| j                  d|j                  d      S )NrN   r   )rr   r7   r   binasciihexlifyhashpwr   r   )rm   r    r6   r   r   s        r   r7   z!BCryptSHA256PasswordHasher.encode  sr    ##%??$ ;;"''H(=(D(D(FGH}}Xt,..$++g*>??r   c                     |j                  dd      \  }}}}}|| j                  k(  sJ |||dd  |d d t        |      dS )NrN   r      )r   algostrchecksumr6   work_factorr   )rm   r   r   emptyr   r   r   s          r   r   z!BCryptSHA256PasswordHasher.decode   sY    7>}}S!7L4	5';DNN***"RS	"I{+
 	
r   c                     |j                  dd      \  }}|| j                  k(  sJ | j                  ||j                  d            }t        ||      S )NrN   rS   r   )rU   r   r7   r   )rm   r    r   r   r   r   s         r   r   z!BCryptSHA256PasswordHasher.verify  sN    !--Q/	4DNN***KK$++g*>?	$Wi88r   c                     | j                  |      }t        d      |d   t        d      |d   t        d      t        |d         t        d      t        |d         iS )Nr   work factorr   r6   r   r   r   s      r   r   z'BCryptSHA256PasswordHasher.safe_summary  s\    ++g&kNGK0mgm4fIy1jM9WZ%89	
 	
r   c                 H    | j                  |      }|d   | j                  k7  S )Nr   )r   r   r   s      r   r   z&BCryptSHA256PasswordHasher.must_update  s$    ++g&}%44r   c                     |j                  dd      \  }}|d d }|j                  d      d   }d| j                  t        |      z
  z  dz
  }|dkD  r-| j                  ||j                  d             |dz  }|dkD  r,y y )NrN   rS      r   r   r   )rU   r   r   r7   )rm   r    r   r   r   r6   r   diffs           r   r   z)BCryptSHA256PasswordHasher.harden_runtime  s    --Q'4CRyC#T[[3v;./!3QhKK$++g"67AID Qhr   N)rl   r   r5   r   r   r   r   r   re   r   r6   r7   r   r   r   r   r   r   r   r   r   r     sE      I^^F"GF+
@	
9
5r   r   c                       e Zd ZdZdZdZy)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)rl   r   r5   r   r   r   r   r   r   r   r   )  s     IFr   r   c                   J    e Zd ZdZdZdZdZdZdZddZ	d	 Z
d
 Zd Zd Zd Zy)ScryptPasswordHasherz=
    Secure password hashing using the Scrypt algorithm.
    scryptr   r      i @  Nc           	         | j                  ||       |xs | j                  }|xs | j                  }|xs | j                  }t	        j
                  |j                         |j                         |||| j                  d      }t        j                  |      j                  d      j                         }d| j                  |||||fz  S )N@   )r6   nrpmaxmemdklenr   z%s$%d$%s$%d$%d$%s)r~   r   
block_sizer   r   r   r7   r   r   r   r   r   r   )rm   r    r6   r   r   r   hash_s          r   r7   zScryptPasswordHasher.encodeF  s    $/!!! !!!OO;;
   '..w7==?"dnnaq!U%KKKr   c                     |j                  dd      \  }}}}}}|| j                  k(  sJ |t        |      |t        |      t        |      |dS )NrN      )r   r   r6   r   r   rW   r   )rm   r   r   r   r6   r   r   r   s           r   r   zScryptPasswordHasher.decodeW  sa    GN}}H
D	;j+u DNN***"{+j/{+
 	
r   c                 ~    | j                  |      }| j                  ||d   |d   |d   |d         }t        ||      S )Nr6   r   r   r   r   r   s        r   r   zScryptPasswordHasher.verifye  sO    ++g&KKFOM"L!M"
	 %Wi88r   c                     | j                  |      }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
block sizer   r   r6   rW   r   r   s      r   r   z!ScryptPasswordHasher.safe_summaryp  sx    ++g&kNGK0mgm4lOW\2mgm4fIy1fIy1
 	
r   c                     | j                  |      }|d   | j                  k7  xs& |d   | j                  k7  xs |d   | j                  k7  S )Nr   r   r   )r   r   r   r   r   s      r   r   z ScryptPasswordHasher.must_update{  sV    ++g&M"d&6&66 :|$7:}%)9)99	
r   c                      y rA   r   r{   s      r   r   z#ScryptPasswordHasher.harden_runtime  r   r   )NNN)rl   r   r5   r   r   r   r   r   r   r7   r   r   r   r   r   r   r   r   r   r   ;  sA     IJFKKL"
	9	

r   r   c                   8    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
y	)
MD5PasswordHasherzE
    The Salted MD5 password hashing algorithm (not recommended)
    md5c                     | j                  ||       t        j                  ||z   j                               j	                         }| j
                  d|d|S )NrN   )r~   r   r  r7   	hexdigestr   )rm   r    r6   rW   s       r   r7   zMD5PasswordHasher.encode  sG    $/{{D8O3356@@B!^^T488r   c                 \    |j                  dd      \  }}}|| j                  k(  sJ |||dS )NrN   r   )r   rW   r6   )rU   r   )rm   r   r   r6   rW   s        r   r   zMD5PasswordHasher.decode  s>     'c1 5	4DNN***"
 	
r   c                 f    | j                  |      }| j                  ||d         }t        ||      S Nr6   r   r   s        r   r   zMD5PasswordHasher.verify  s1    ++g&KK'&/:	$Wi88r   c           	          | j                  |      }t        d      |d   t        d      t        |d   d      t        d      t        |d         iS )Nr   r6   r   )rX   rW   r   r   s      r   r   zMD5PasswordHasher.safe_summary  sN    ++g&kNGK0fIyq9fIy1
 	
r   c                 V    | j                  |      }t        |d   | j                        S r  )r   r`   rv   r   s      r   r   zMD5PasswordHasher.must_update  s'    ++g&1B1BCCr   c                      y rA   r   r{   s      r   r   z MD5PasswordHasher.harden_runtime  s    r   N)rl   r   r5   r   r   r7   r   r   r   r   r   r   r   r   r  r    s-     I9

9

Dr   r  )rI   )NrI   )r  *)0r   r   	functoolsr   rh   r]   r   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,   r.   r   	lru_cacher?   rB   rG   r   r   r[   r`   rb   r   r   r   r   r   r   r  r   r   r   <module>r     s9            7 / $  6 6   
O!#H), 
 
 B B 
// /2!,N
h
 h
V5E- 5Ep	3 	c
- c
LE!3 EP5 $K- K\'* 'r   