
    rhD"                     F   S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
  SSKJr  SSKJr  SSKJr  \" S5      rS	r " S
 S\5      r " S S\5      rS rS rS rS rSS jrS rS S jr " S S5      rSS\S4S jrSS\SS4S jr " S S5      r  " S S\ 5      r!g)!a_  
Functions for creating and restoring url-safe signed JSON objects.

The format used looks like this:

>>> signing.dumps("hello")
'ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk'

There are two components here, separated by a ':'. The first component is a
URLsafe base64 encoded JSON of the object passed to dumps(). The second
component is a base64 encoded hmac/SHA-256 hash of "$first_component:$secret"

signing.loads(s) checks the signature and returns the deserialized object.
If the signature fails, a BadSignature exception is raised.

>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk")
'hello'
>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv42-modified")
...
BadSignature: Signature "ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv42-modified" does not match

You can optionally compress the JSON prior to base64 encoding it to save
space, using the compress=True argument. This checks if compression actually
helps and only applies compression if the result is a shorter string:

>>> signing.dumps(list(range(1, 20)), compress=True)
'.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ'

The fact that the string is compressed is signalled by the prefixed '.' at the
start of the base64 JSON.

There are 65 url-safe characters: the 64 used by url-safe base64 and the ':'.
These functions make use of all of them.
    N)settings)constant_time_comparesalted_hmacforce_bytes)import_string)_lazy_re_compilez^[A-z0-9-_=]*$>0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzc                       \ rS rSrSrSrg)BadSignature4   zSignature does not match. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       K/var/www/html/shao/venv/lib/python3.13/site-packages/django/core/signing.pyr   r   4   s    #r   r   c                       \ rS rSrSrSrg)SignatureExpired:   z3Signature timestamp is older than required max_age.r   Nr   r   r   r   r   r   :   s    =r   r   c                     U S:X  a  gU S:  a  SOSn[        U 5      n SnU S:  a"  [        U S5      u  p[        U   U-   nU S:  a  M"  X-   $ )Nr   0- >   )absdivmodBASE62_ALPHABET)ssignencoded	remainders       r   
b62_encoder'   @   s]    Ava%3RDAAG
a%a}!),w6 a% >r   c                     U S:X  a  gSnU S   S:X  a  U SS  n SnSnU  H  nUS-  [         R                  U5      -   nM      X-  $ )Nr   r      r   r   )r"   index)r#   r$   decodeddigits       r   
b62_decoder.   L   s\    CxDts{abEGB,!6!6u!== >r   c                 L    [         R                  " U 5      R                  S5      $ )N   =)base64urlsafe_b64encodestrip)r#   s    r   
b64_encoder4   Y   s    ##A&,,T22r   c                 V    S[        U 5      * S-  -  n[        R                  " X-   5      $ )Nr0      )lenr1   urlsafe_b64decode)r#   pads     r   
b64_decoder:   ]   s)    
3q6'A+
C##AG,,r   c           	      `    [        [        XX#S9R                  5       5      R                  5       $ )N	algorithm)r4   r   digestdecode)saltvaluekeyr=   s       r   base64_hmacrC   b   s)    D:AACfhr   c                     S[        U 5      -   $ )Ns   django.http.cookiesr   )rB   s    r   _cookie_signer_keyrE   h   s    !K$444r   c                     [        [        R                  5      nU" [        [        R                  5      [        [        [        R                  5      U S9$ )N)rB   fallback_keysr@   )r   r   SIGNING_BACKENDrE   
SECRET_KEYmapSECRET_KEY_FALLBACKS)r@   Signers     r   get_cookie_signerrM   m   sA    8334Fx223,h.K.KL r   c                   $    \ rS rSrSrS rS rSrg)JSONSerializerv   zK
Simple wrapper around json to be used in signing.dumps and
signing.loads.
c                 J    [         R                  " USS9R                  S5      $ )N),:)
separatorslatin-1)jsondumpsencode)selfobjs     r   rW   JSONSerializer.dumps|   s    zz#*5<<YGGr   c                 L    [         R                  " UR                  S5      5      $ )NrU   )rV   loadsr?   )rY   datas     r   r]   JSONSerializer.loads   s    zz$++i011r   r   N)r   r   r   r   r   rW   r]   r   r   r   r   rO   rO   v   s    
H2r   rO   zdjango.core.signingFc                 0    [        XS9R                  XUS9$ )a  
Return URL-safe, hmac signed base64 compressed JSON string. If key is
None, use settings.SECRET_KEY instead. The hmac algorithm is the default
Signer algorithm.

If compress is True (not the default), check if compressing using zlib can
save some space. Prepend a '.' to signify compression. This is included
in the signature, to protect against zip bombs.

Salt can be used to namespace the hash, so that a signed string is
only valid for a given namespace. Leaving this at the default
value or re-using a salt value across different parts of your
application without good cause is a security risk.

The serializer is expected to return a bytestring.
)rB   r@   )
serializercompress)TimestampSignersign_object)rZ   rB   r@   ra   rb   s        r   rW   rW      s&    & s.::X ;  r   c                 4    [        XUS9R                  U UUS9$ )zp
Reverse of dumps(), raise BadSignature if signature fails.

The serializer is expected to accept a bytestring.
)rB   r@   rG   )ra   max_age)rc   unsign_object)r#   rB   r@   ra   rf   rG   s         r   r]   r]      s.     -m	  r   c                   Z    \ rS rSrSSSSSS.S jrSS jrS rS r\S	4S
 jr	\4S jr
Srg)rL      NrS   )rB   sepr@   r=   rG   c                   U=(       d    [         R                  U l        Ub  UO[         R                  U l        X l        U=(       d1    U R                  R                  < SU R                  R                  < 3U l	        U=(       d    SU l
        [        R                  U R
                  5      (       a  [        SU-  5      eg )N.sha256zJUnsafe Signer separator: %r (cannot be empty or consist of only A-z0-9-_=))r   rI   rB   rK   rG   rj   	__class__r   r   r@   r=   _SEP_UNSAFEmatch
ValueError)rY   rB   rj   r@   r=   rG   s         r   __init__Signer.__init__   s     -(-- ( .. 	
  
NN%%NN##
	 #.hTXX&&"$'(  'r   c                 p    U=(       d    U R                   n[        U R                  S-   XU R                  S9$ )Nsignerr<   )rB   rC   r@   r=   )rY   rA   rB   s      r   	signatureSigner.signature   s+    oTXX499x/t~~VVr   c                 J    U< U R                   < U R                  U5      < 3$ N)rj   rv   )rY   rA   s     r   r$   Signer.sign   s     $((DNN5,ABBr   c                 0   U R                   U;  a  [        SU R                   -  5      eUR                  U R                   S5      u  p#U R                  /U R                  Q H&  n[        X0R                  X$5      5      (       d  M$  Us  $    [        SU-  5      e)NzNo "%s" found in valuer)   zSignature "%s" does not match)rj   r   rsplitrB   rG   r   rv   )rY   signed_valuerA   sigrB   s        r   unsignSigner.unsign   s    88<'7$((BCC!((15
HH2t112C$S..*DEE 3 :S@AAr   Fc                    U" 5       R                  U5      nSnU(       a5  [        R                  " U5      n[        U5      [        U5      S-
  :  a  UnSn[	        U5      R                  5       nU(       a  SU-   nU R                  U5      $ )a5  
Return URL-safe, hmac signed base64 compressed JSON string.

If compress is True (not the default), check if compressing using zlib
can save some space. Prepend a '.' to signify compression. This is
included in the signature, to protect against zip bombs.

The serializer is expected to return a bytestring.
Fr)   Trl   )rW   zlibrb   r7   r4   r?   r$   )rY   rZ   ra   rb   r^   is_compressed
compressedbase64ds           r   rd   Signer.sign_object   sx     |!!#&t,J:#d)a-0! $T"))+GmGyy!!r   c                     U R                   " U40 UD6R                  5       nUS S S:H  nU(       a  USS  n[        U5      nU(       a  [        R                  " U5      nU" 5       R                  U5      $ )Nr)      .)r   rX   r:   r   
decompressr]   )rY   
signed_objra   kwargsr   r   r^   s          r   rg   Signer.unsign_object   sl     ++j3F3::<Ra[D(
abkG'"??4(D|!!$''r   )r=   rG   rB   r@   rj   ry   )r   r   r   r   rr   rv   r$   r   rO   rd   rg   r   r   r   r   rL   rL      s;    sT*WCB +95 "2 4B (r   rL   c                   >   ^  \ rS rSrS rU 4S jrSU 4S jjrSrU =r$ )rc      c                 P    [        [        [        R                  " 5       5      5      $ ry   )r'   inttime)rY   s    r   	timestampTimestampSigner.timestamp   s    #diik*++r   c                 h   > U< U R                   < U R                  5       < 3n[        TU ]  U5      $ ry   )rj   r   superr$   )rY   rA   rn   s     r   r$   TimestampSigner.sign  s)    !488T^^-=>w|E""r   c                 >  > [         TU ]  U5      nUR                  U R                  S5      u  p[	        U5      nUb`  [        U[        R                  5      (       a  UR                  5       n[        R                  " 5       U-
  nXR:  a  [        SU< SU< S35      eU$ )zS
Retrieve original value and check it wasn't signed more
than max_age seconds ago.
r)   zSignature age z > z seconds)r   r   r|   rj   r.   
isinstancedatetime	timedeltatotal_secondsr   r   )rY   rA   rf   resultr   agern   s         r   r   TimestampSigner.unsign  s    
 &!==15y)	'8#5#566!//1))+	)C}&#w'WXXr   r   ry   )	r   r   r   r   r   r$   r   r   __classcell__)rn   s   @r   rc   rc      s    ,# r   rc   )sha1)z%django.core.signing.get_cookie_signer)"r   r1   r   rV   r   r   django.confr   django.utils.cryptor   r   django.utils.encodingr   django.utils.module_loadingr   django.utils.regex_helperr	   ro   r"   	Exceptionr   r   r'   r.   r4   r:   rC   rE   rM   rO   rW   r]   rL   rc   r   r   r   <module>r      s   !F        B - 5 601R	9 		| 		
3-
5

2 
2 -.SX4 		,J( J(Zf r   