o
    ˜¨ÊhË  ã                   @   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ƒrOejZejZdS 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
   úE/var/www/html/env/lib/python3.10/site-packages/django/utils/crypto.pyr      s    r   Úsha1)Ú	algorithmc             
   C   sx   |du rt j}t| ƒ} t|ƒ}ztt|ƒ}W n ty) } ztd| ƒ|‚d}~ww || | ƒ ¡ }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   Ú
SECRET_KEYr   ÚgetattrÚhashlibÚAttributeErrorr   ÚdigestÚhmacÚnew)Úkey_saltÚvalueÚsecretr   ÚhasherÚeÚkeyr
   r
   r   Úsalted_hmac   s    ÿþ€ÿr   Ú>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>>   s   € 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   )Úval1Ú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ÚsaltÚ
iterationsÚdklenr   r
   r
   r   Úpbkdf2F   s   r7   Úusedforsecurityó    T)r8   c                C   ó
   t  | ¡S r    )r   Úmd5)Údatar8   r
   r
   r   r;   Y   ó   
r;   c                C   r:   r    )r   r   )Úhash_algorithmr8   r
   r
   r   Únew_hash\   r=   r?   r    )r   N)r9   )r	   r   r   r!   Údjango.confr   Údjango.utils.encodingr   Údjango.utils.inspectr   Ú
ValueErrorr   r   ÚRANDOM_STRING_CHARSr+   r/   r7   r;   r   r?   r
   r
   r
   r   Ú<module>   s$    

