
 X                 @   s  d  Z  d d l m 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 y e j   Z d Z Wn1 e k
 r d d l Z e j d	  d
 Z Yn Xd d d  Z d d d d  Z e e d  r'd d   Z n d d   Z d d   Z d d   Z e e d  rod d d d  Z n d d d d  Z d S)z3
Django's standard crypto functions and utilities.
    )unicode_literalsN)settings)six)force_bytes)rangeTzjA secure pseudo-random number generator is not available on your system. Falling back to Mersenne Twister.Fc             C   sk   | d k r t  j } n  t |   }  t |  } t j |  |  j   } t j | d t |  d t j S)z
    Returns the HMAC-SHA1 of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY).

    A different key_salt should be passed in for every application of HMAC.
    NmsgZ	digestmod)r   
SECRET_KEYr   hashlibsha1digesthmacnew)Zkey_saltvalueZsecretkey r   A/home/ubuntu/projects/ifolica/build/django/django/utils/crypto.pysalted_hmac   s    r      Z>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789c                sp   t  sJ t j t j d t j   t j   t j f j	 d   j
    n  d j   f d d   t |   D  S)z
    Returns a securely generated random string.

    The default length of 12 with the a-z, A-Z, 0-9 character set returns
    a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
    z%s%s%szutf-8 c             3   s   |  ] } t  j    Vq d  S)N)randomchoice).0i)allowed_charsr   r   	<genexpr>M   s    z$get_random_string.<locals>.<genexpr>)using_sysrandomr   seedr	   sha256getstatetimer   r   encoder   joinr   )lengthr   r   )r   r   get_random_string6   s    			#r#   compare_digestc             C   s   t  j t |   t |   S)N)r   r$   r   )val1val2r   r   r   constant_time_compareR   s    r'   c             C   s   t  |   t  |  k r d Sd } t j rz t |  t  rz t | t  rz xh t |  |  D] \ } } | | | AO} qY Wn: x7 t |  |  D]& \ } } | t |  t |  AO} q W| d k S)a  
        Returns True if the two strings are equal, False otherwise.

        The time taken is independent of the number of characters that match.

        For the sake of simplicity, this function executes in constant time only
        when the two strings have the same length. It short-circuits when they
        have different lengths. Since Django only uses it to compare hashes of
        known expected length, this is acceptable.
        Fr   )lenr   PY3
isinstancebyteszipord)r%   r&   resultxyr   r   r   r'   U   s    'c             C   s   t  t j |   d  S)zq
    Convert a binary string into a long integer

    This is a clever optimization for fast xor vector math
       )intbinasciihexlify)r/   r   r   r   _bin_to_longl   s    r5   c             C   s   t  j | |  j d   S)zw
    Convert a long integer into a binary string.
    hex_format_string is like "%020x" for padding 10 characters.
    ascii)r3   	unhexlifyr    )r/   hex_format_stringr   r   r   _long_to_binu   s    r9   pbkdf2_hmacc             C   s^   | d k r t  j } n  | s' d } n  t |   }  t |  } t  j |   j |  | | |  S)z
        Implements PBKDF2 with the same API as Django's existing
        implementation, using the stdlib.

        This is used in Python 2.7.8+ and 3.4+.
        N)r	   r   r   r:   name)passwordsalt
iterationsdklenr   r   r   r   pbkdf2~   s    	r@   c       	         s   d k s t   | s$ t j } n  t |   }  t    |   j } | sW | } n  | d | k rv t d   n  | | } | | d | } d | d  |   |     t |    j k r | |   j   }  n  |  d  j t |   7}   j	 |  j
 t j    j	 |  j
 t j        f d d	       f d
 d   t d |  D } d j |    |  d |  S)a  
        Implements PBKDF2 as defined in RFC 2898, section 5.2

        HMAC+SHA256 is used as the default pseudo random function.

        As of 2014, 100,000 iterations was the recommended default which took
        100ms on a 2.7Ghz Intel i7 with an optimized implementation. This is
        probably the bare minimum for security given 1000 iterations was
        recommended in 2001. This code is very well optimized for CPython and
        is about five times slower than OpenSSL's implementation. Look in
        django.contrib.auth.hashers for the present default, it is lower than
        the recommended 100,000 because of the performance difference between
        this and an optimized implementation.
        r             zdklen too bigz%%0%ixs    c                s    t  j d |   } d } xo t t    D][ }  j    j   } } | j |  | j | j    | j   } | t |  N} q/ Wt |    S)Ns   >Ir   )	structpackr   r2   copyupdater   r5   r9   )r   ur.   jZdig1Zdig2)r8   innerr>   outerr=   r   r   F   s    zpbkdf2.<locals>.Fc                s   g  |  ] }   |   q Sr   r   )r   r/   )rL   r   r   
<listcomp>   s   	 zpbkdf2.<locals>.<listcomp>    Nl        l    )AssertionErrorr	   r   r   digest_sizeOverflowErrorr(   
block_sizer   rG   	translater   Ztrans_36Ztrans_5Cr   r!   )	r<   r=   r>   r?   r   ZhlenlrTr   )rL   r8   rJ   r>   rK   r=   r   r@      s,    	")__doc__
__future__r   r3   r	   r   r   rD   r   Zdjango.confr   Zdjango.utilsr   Zdjango.utils.encodingr   Zdjango.utils.six.movesr   SystemRandomr   NotImplementedErrorwarningswarnr   r#   hasattrr'   r5   r9   r@   r   r   r   r   <module>   s:   
	