a
    LcòfË  ã                   @   s¾   d 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	 G dd„ de
ƒZddd	œd
d„ZdZefdd„Zdd„ Zddd„Ze	ejdƒrœejZejZndddœdd„Zddœdd„ZdS )z3
Django's standard crypto functions and utilities.
é    N)Úsettings)Úforce_bytes)Úfunc_supports_parameterc                   @   s   e Zd ZdZdS )ÚInvalidAlgorithmz&Algorithm is not supported by hashlib.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r
   r
   úP/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/utils/crypto.pyr      s   r   Úsha1)Ú	algorithmc             
   C   s„   |du rt j}t| ƒ} t|ƒ}ztt|ƒ}W n2 ty^ } ztd| ƒ|‚W Y d}~n
d}~0 0 || | ƒ ¡ }tj	|t|ƒ|d�S )a*  
    Return the HMAC of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY). Default algorithm is SHA1,
    but any algorithm name supported by hashlib can be passed.

    A different key_salt should be passed in for every application of HMAC.
    Nz6%r is not an algorithm accepted by the hashlib module.)ÚmsgÚ	digestmod)
r   Z
SECRET_KEYr   ÚgetattrÚhashlibÚAttributeErrorr   ÚdigestÚhmacÚnew)Zkey_saltÚvalueÚsecretr   ZhasherÚeÚkeyr
   r
   r   Úsalted_hmac   s    ÿþr   Z>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789c                    s   d  ‡ fdd„t| ƒD ƒ¡S )aG  
    Return a securely generated random string.

    The bit length of the returned value can be calculated with the formula:
        log_2(len(allowed_chars)^length)

    For example, with default `allowed_chars` (26+26+10), this gives:
      * length: 12, bit length =~ 71 bits
      * length: 22, bit length =~ 131 bits
    Ú c                 3   s   | ]}t  ˆ ¡V  qd S ©N)ÚsecretsÚchoice)Ú.0Úi©Úallowed_charsr
   r   Ú	<genexpr>>   ó    z$get_random_string.<locals>.<genexpr>)ÚjoinÚrange)Úlengthr"   r
   r!   r   Úget_random_string3   s    r(   c                 C   s   t  t| ƒt|ƒ¡S )z:Return True if the two strings are equal, False otherwise.)r   Úcompare_digestr   )Zval1Zval2r
   r
   r   Úconstant_time_compareA   s    r*   c                 C   s<   |du rt j}|pd}t| ƒ} t|ƒ}t  |ƒ j| |||¡S )z)Return the hash of password using pbkdf2.N)r   Úsha256r   Úpbkdf2_hmacÚname)ÚpasswordÚsaltZ
iterationsZdklenr   r
   r
   r   Úpbkdf2F   s    r0   Úusedforsecurityr$   T)r1   c                C   s
   t  | ¡S r   )r   Úmd5)Údatar1   r
   r
   r   r2   Y   s    r2   c                C   s
   t  | ¡S r   )r   r   )Zhash_algorithmr1   r
   r
   r   Únew_hash\   s    r4   )N)r   N)r$   )r	   r   r   r   Zdjango.confr   Zdjango.utils.encodingr   Zdjango.utils.inspectr   Ú
ValueErrorr   r   ZRANDOM_STRING_CHARSr(   r*   r0   r2   r   r4   r
   r
   r
   r   Ú<module>   s"   
