U
    8ÄT_   ã                   @   s’   d 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
 G dd„ deƒZddd	œd
d„Zeƒ Zedfdd„Zdd„ Zddd„ZdS )z3
Django's standard crypto functions and utilities.
é    N)Úsettings)ÚRemovedInDjango40Warning)Úforce_bytesc                   @   s   e Zd ZdZdS )ÚInvalidAlgorithmz&Algorithm is not supported by hashlib.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r
   r
   ú7/tmp/pip-unpacked-wheel-dv63caxw/django/utils/crypto.pyr      s   r   Úsha1)Ú	algorithmc             
   C   s‚   |dkrt j}t| ƒ} t|ƒ}ztt|ƒ}W n0 tk
r\ } ztd| ƒ|‚W 5 d}~X Y nX || | ƒ ¡ }tj	|t|ƒ|d�S )a0  
    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.new() 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                    s4   | t krt dt¡ d} 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
    z.Not providing a length argument is deprecated.é   Ú c                 3   s   | ]}t  ˆ ¡V  qd S )N)ÚsecretsÚchoice)Ú.0Úi©Úallowed_charsr
   r   Ú	<genexpr>J   s     z$get_random_string.<locals>.<genexpr>)ÚNOT_PROVIDEDÚwarningsÚwarnr   ÚjoinÚrange)Úlengthr"   r
   r!   r   Úget_random_string6   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_compareM   s    r,   c                 C   s<   |dkrt j}|pd}t| ƒ} t|ƒ}t  |ƒ j| |||¡S )z)Return the hash of password using pbkdf2.N)r   Úsha256r   Úpbkdf2_hmacÚname)ÚpasswordZsaltZ
iterationsZdklenr   r
   r
   r   Úpbkdf2R   s    r1   )N)r   N)r	   r   r   r   r%   Zdjango.confr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   Ú
ValueErrorr   r   Úobjectr$   r*   r,   r1   r
   r
   r
   r   Ú<module>   s   ÿ
