
    ,heW                     2   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZmZmZmZ  ed	d
      Z  edd      Z!ddZ"ddZ# G d de$      Z%d Z&d Z'ddZ(ddZ)d Z*d Z+d Z,ddZ-d Z.d dZ/y)!    N)reduce)tobytesbord_copy_bytes
iter_rangetostrbchrbstr)SHA1SHA256HMACCMACBLAKE2s)strxor)get_random_bytes)sizelong_to_bytesbytes_to_long)load_pycryptodome_raw_libcreate_string_bufferget_raw_bufferc_size_tzCrypto.Cipher._Salsa20z
                    int Salsa20_8_core(const uint8_t *x, const uint8_t *y,
                                       uint8_t *out);
                    zCrypto.Protocol._scrypta  
                    typedef int (core_t)(const uint8_t [64], const uint8_t [64], uint8_t [64]);
                    int scryptROMix(const uint8_t *data_in, uint8_t *data_out,
                           size_t data_len, unsigned N, core_t *core);
                    c                 b   |st         }t        |       } |j                  | |z         }|j                  }||kD  rt	        d|z        t        |      dk7  rt        dt        |      z        t        |dz
        D ]!  }|j                  |j                               }# |j                         d| S )aM  Derive one key from a password (or passphrase).

    This function performs key derivation according to an old version of
    the PKCS#5 standard (v1.5) or `RFC2898
    <https://www.ietf.org/rfc/rfc2898.txt>`_.

    Args:
     password (string):
        The secret password to generate the key from.
     salt (byte string):
        An 8 byte string to use for better protection from dictionary attacks.
        This value does not need to be kept secret, but it should be randomly
        chosen for each derivation.
     dkLen (integer):
        The length of the desired key. The default is 16 bytes, suitable for
        instance for :mod:`Crypto.Cipher.AES`.
     count (integer):
        The number of iterations to carry out. The recommendation is 1000 or
        more.
     hashAlgo (module):
        The hash algorithm to use, as a module or an object from the :mod:`Crypto.Hash` package.
        The digest length must be no shorter than ``dkLen``.
        The default algorithm is :mod:`Crypto.Hash.SHA1`.

    Return:
        A byte string of length ``dkLen`` that can be used as key.
    z:Selected hash algorithm has a too short digest (%d bytes).   z,Salt is not 8 bytes long (%d bytes instead).   N)	r   r   newdigest_size	TypeErrorlen
ValueErrorr   digest)passwordsaltdkLencounthashAlgopHashr!   is           U/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Protocol/KDF.pyPBKDF1r*   9   s    : x HLL$'EFv~TW]]^^
4yA~G#d)STTa  *		%,,.)*<<>&5!!    c                    
 t                t        |      }rrt        d      t        st        d      s~fd fd
d}d}t	        |      |k  r  |t        j                  d|      z         gd	z  |t        t        
fd
t        |      D              z  }|dz  }t	        |      |k  r^nd}d}t	        |      |k  rt        j                   d      }|j                         j                  |t        j                  d|      z         j                         }	||j                  |	|      z  }|dz  }t	        |      |k  r|d| S )a  Derive one or more keys from a password (or passphrase).

    This function performs key derivation according to the PKCS#5 standard (v2.0).

    Args:
     password (string or byte string):
        The secret password to generate the key from.

        Strings will be encoded as ISO 8859-1 (also known as Latin-1),
        which does not allow any characters with codepoints > 255.
     salt (string or byte string):
        A (byte) string to use for better protection from dictionary attacks.
        This value does not need to be kept secret, but it should be randomly
        chosen for each derivation. It is recommended to use at least 16 bytes.

        Strings will be encoded as ISO 8859-1 (also known as Latin-1),
        which does not allow any characters with codepoints > 255.
     dkLen (integer):
        The cumulative length of the keys to produce.

        Due to a flaw in the PBKDF2 design, you should not request more bytes
        than the ``prf`` can output. For instance, ``dkLen`` should not exceed
        20 bytes in combination with ``HMAC-SHA1``.
     count (integer):
        The number of iterations to carry out. The higher the value, the slower
        and the more secure the function becomes.

        You should find the maximum number of iterations that keeps the
        key derivation still acceptable on the slowest hardware you must support.

        Although the default value is 1000, **it is recommended to use at least
        1000000 (1 million) iterations**.
     prf (callable):
        A pseudorandom function. It must be a function that returns a
        pseudorandom byte string from two parameters: a secret and a salt.
        The slower the algorithm, the more secure the derivation function.
        If not specified, **HMAC-SHA1** is used.
     hmac_hash_module (module):
        A module from ``Crypto.Hash`` implementing a Merkle-Damgard cryptographic
        hash, which PBKDF2 must use in combination with HMAC.
        This parameter is mutually exclusive with ``prf``.

    Return:
        A byte string of length ``dkLen`` that can be used as key material.
        If you want multiple keys, just break up this string into segments of the desired length.
    z2'prf' and 'hmac_hash_module' are mutually exlusiveN_pbkdf2_hmac_assistc                 N    t        j                  | |      j                         S Nr   r   r!   )pshmac_hash_modules     r)   <lambda>zPBKDF2.<locals>.<lambda>   s    txx1.>?FFH r+   c                 >    | d    | d         c| d<   | d<   | d   S )Nr   r    )r2   r"   prfs    r)   linkzPBKDF2.<locals>.link   s.    1s8QqT2JAaD!A$Q4Kr+   r+   r   z>I   c              3   .   K   | ]  }         y wr/   r6   ).0jr8   r2   s     r)   	<genexpr>zPBKDF2.<locals>.<genexpr>   s     "Aq47"As   )r   r    r   hasattrr   structpackr   r   ranger   r   copyupdater!   r-   )r"   r#   r$   r%   r7   r3   keyr(   basefirst_digestr8   r2   s   `   ``    @@r)   PBKDF2rG   d   s_   ` x H4=D
MNN
{'/
'*,AB ;HC	 #hXtfkk$&::;<q@A6&"AE%L"ABBCFA #h #h88Hc+;<D99;--dV[[q5I.IJQQSL4++L%@@CFA	 #h v;r+   c                   :    e Zd ZdZddZed        Zd Zd Zd Z	y)	_S2VzString-to-vector PRF as defined in `RFC5297`_.

    This class implements a pseudorandom function family
    based on CMAC that takes as input a vector of strings.

    .. _RFC5297: http://tools.ietf.org/html/rfc5297
    Nc                     t        dd|      | _        || _        d|j                  z  x| _        | _        |j                  dz  dz
  | _        |i | _        yt        |      | _        y)a~  Initialize the S2V PRF.

        :Parameters:
          key : byte string
            A secret that can be used as key for CMACs
            based on ciphers from ``ciphermod``.
          ciphermod : module
            A block cipher module from `Crypto.Cipher`.
          cipher_params : dictionary
            A set of extra parameters to use to create a cipher instance.
        N    r   r   )	r   _key
_ciphermod
block_size_last_string_cache
_n_updates_cipher_paramsdict)selfrD   	ciphermodcipher_paramss       r)   __init__z_S2V.__init__   si      dC0	#*1I4H4H*HHDK $..2Q6 "$D"&}"5Dr+   c                     t        | |      S )a  Create a new S2V PRF.

        :Parameters:
          key : byte string
            A secret that can be used as key for CMACs
            based on ciphers from ``ciphermod``.
          ciphermod : module
            A block cipher module from `Crypto.Cipher`.
        )rI   )rD   rU   s     r)   r   z_S2V.new   s     C##r+   c                     t        |      dz  }t        |d         dz  r|dz  }t        |t        |            t        |       d  S )Nr   r         )r   r   r   r   )rT   bsdoubleds      r)   _doublez_S2V._double   sH    #q(1;tOGWc"g.Bxy99r+   c                 x   | j                   dk(  rt        d      | xj                   dz  c_         t        j                  | j                  | j
                  | j                  | j                        }t        | j                  | j                        |j                               | _
        t        dd|      | _        y)ad  Pass the next component of the vector.

        The maximum number of components you can pass is equal to the block
        length of the cipher (in bits) minus 1.

        :Parameters:
          item : byte string
            The next component of the vector.
        :Raise TypeError: when the limit on the number of components has been reached.
        r   z!Too many components passed to S2Vr   msgrU   rV   N)rQ   r   r   r   rL   rO   rM   rR   r   r^   rP   r!   r   )rT   itemmacs      r)   rC   z_S2V.update   s     ??a?@@1hhtyy,,!%%)%8%8: T\\$++6

E'dD9r+   c                    t        | j                        dk\  r4| j                  dd t        | j                  dd | j                        z   }n:| j                  dz   dz   dd }t        || j	                  | j                              }t        j                  | j                  || j                  | j                        }|j                         S )z"Derive a secret from the vector of components.

        :Return: a byte string, as long as the block length of the cipher.
           Ni   s                  r`   )r   rO   r   rP   r^   r   r   rL   rM   rR   r!   )rT   finalpaddedrc   s       r)   derivez_S2V.derive  s     t  !R'%%ds+fT5F5Fst5Ldkk.ZZE '''1L@#2FF64<<#<=Ehhtyy !%%)%8%8: zz|r+   r/   )
__name__
__module____qualname____doc__rW   staticmethodr   r^   rC   ri   r6   r+   r)   rI   rI      s/    62 
$ 
$::.r+   rI   c                 R    t        j                  | ||      j                         }|S )N	digestmodr0   )r#   ikmhashmodprks       r)   _HKDF_extractru     s"    
((4
0
7
7
9CJr+   c                 "   dg}d}d}||k  rnt        j                  | |d   |z   t        j                  d|      z   |      }|j	                  |j                                ||j                  z  }|dz  }||k  rndj                  |      }|d | S )Nr+   r   r   Brp   )r   r   r?   r@   appendr!   r   join)	rt   infoLrs   tntlenhmacokms	            r)   _HKDF_expandr      s    	A	AD
(xxQrUT\FKKQ,??7S	###	Q	 (
 ((1+Cr7Nr+   c                     ||z  }|d|j                   z  kD  rt        d      |sd|j                   z  }|d}t        || |      }t        ||||      }|dk(  r|d| S t	        d||      D 	cg c]
  }	||	|	|z     }
}	t        |
d|       S c c}	w )a  Derive one or more keys from a master secret using
    the HMAC-based KDF defined in RFC5869_.

    Args:
     master (byte string):
        The unguessable value used by the KDF to generate the other keys.
        It must be a high-entropy secret, though not necessarily uniform.
        It must not be a password.
     key_len (integer):
        The length in bytes of every derived key.
     salt (byte string):
        A non-secret, reusable value that strengthens the randomness
        extraction step.
        Ideally, it is as long as the digest size of the chosen hash.
        If empty, a string of zeroes in used.
     hashmod (module):
        A cryptographic hash algorithm from :mod:`Crypto.Hash`.
        :mod:`Crypto.Hash.SHA512` is a good choice.
     num_keys (integer):
        The number of keys to derive. Every key is :data:`key_len` bytes long.
        The maximum cumulative length of all keys is
        255 times the digest size.
     context (byte string):
        Optional identifier describing what the keys are used for.

    Return:
        A byte string or a tuple of byte strings.

    .. _RFC5869: http://tools.ietf.org/html/rfc5869
       zToo much secret data to deriverK   Nr+   r   r   )r   r    ru   r   r   list)masterkey_lenr#   rs   num_keyscontext
output_lenrt   r   idxkols              r)   HKDFr   -  s    @ 8#JS7.../9::,,,
fg
.C
sGZ
9C1}8G} J8: s3=! :C :IX:s   ,Bc           	      t   dt        |      dz
  z  |k7  rt        d      |dk\  rt        d      |dd|z  z  kD  rt        d      d	 }t        | ||dz  |z  d|
      }t        j                  }	t
        j                  }
g }t        |      D ]X  }|dz  |z  }t        d|z        } |	|||d|z  z    |t        d|z        ||
      }|rt        d|z        |t        |      gz  }Z t        | dj                  |      ||z  d|
      }|dk(  r|S t        d||z  |      D cg c]
  }||||z     }}|S c c}w )uG  Derive one or more keys from a passphrase.

    Args:
     password (string):
        The secret pass phrase to generate the keys from.
     salt (string):
        A string to use for better protection from dictionary attacks.
        This value does not need to be kept secret,
        but it should be randomly chosen for each derivation.
        It is recommended to be at least 16 bytes long.
     key_len (integer):
        The length in bytes of each derived key.
     N (integer):
        CPU/Memory cost parameter. It must be a power of 2 and less
        than :math:`2^{32}`.
     r (integer):
        Block size parameter.
     p (integer):
        Parallelization parameter.
        It must be no greater than :math:`(2^{32}-1)/(4r)`.
     num_keys (integer):
        The number of keys to derive. Every key is :data:`key_len` bytes long.
        By default, only 1 key is generated.
        The maximum cumulative length of all keys is :math:`(2^{32}-1)*32`
        (that is, 128TB).

    A good choice of parameters *(N, r , p)* was suggested
    by Colin Percival in his `presentation in 2009`__:

    - *( 2¹⁴, 8, 1 )* for interactive logins (≤100ms)
    - *( 2²⁰, 8, 1 )* for file encryption (≤5s)

    Return:
        A byte string or a tuple of byte strings.

    .. __: http://www.tarsnap.com/scrypt/scrypt-slides.pdf
    r9   r   zN must be a power of 2l        zN is too bigl    rZ   zp or r are too bigc                 T    t        j                  | |t              j                         S r/   )r   r   r   r!   )r1   r2   s     r)   r4   zscrypt.<locals>.<lambda>  s    488Aq&#9#@#@#B r+   )r7   zError %X while running scryptr+   r   )bit_sizer    rG   _raw_scrypt_libscryptROMix_raw_salsa20_libSalsa20_8_corer   r   r   r   rz   )r"   r#   r   Nrr1   r   prf_hmac_sha256stage_1r   coredata_outflowr   
buffer_outresultdkr   s                     r)   scryptr   _  s   N 	Xa[1_"122G|((C!G,,-..BOXtQWq[!IG!--K**D H1 
1Sj1n)#'2
WS#a-8'%cAg.!	#
 <vEFF^J/00
1 
"("A#
%B
 1}	 !Gh$6@B c#-  BC BJBs   "D5c                    d}g }| D ]B  }t        t        |            dd  j                  d      }|j                  t	        |             D dj                  |      }t        dt        |      d      D cg c]
  }|||dz     }}g }|d d D ]"  }t        |d      }|j                  ||          $ |d   }t        |d      dt        |      z
  z  }|j                  ||          dj                  |      }t        |      S c c}w )	N@./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789r9   r   r+   r      rw    )
binr   zfillry   r
   rz   rA   r   intr   )	datar2   bitscbits_cr   bits6r   gs	            r)   _bcrypt_encoder     s   JAD "T!Wab!''*DL!" 88D>D(-aTA(>?T#c!e_?E?F3BZ !Qiaf 	b	A
a)CF

#C
MM!C&WWV_F6? @s   5Dc           	         d}g }t        |       D ]A  }|j                  |      }t        |      dd  j                  d      }|j	                  |       C dj                  |      }t        |       dz  }|dk(  rt        d      |dk(  r|d d }n
|d	k(  r|d d
 }t        dt        |      d      D cg c]
  }|||dz     }}g }|D ]&  }	|j	                  t        t        |	d                   ( dj                  |      }|S c c}w )Nr   r9   r   r      r   zIncorrect length   r   r   r+   )r   findr   r   ry   rz   r   r    rA   r	   r   )
r   r2   r   r   r   r   modulo4bits8r   r   s
             r)   _bcrypt_decoder     s   JAD4[ ffQiC""1%E 774=D$i!mG!|+,,	ACRy	ACRy(-aTA(>?T#c!e_?E?F 'd3q!9o&'XXfFM @s   0Dc                    ddl m} t        |       dkD  rt        d      d|cxk  rdk  st        d       t        d      |j	                  | |j
                  |||      }|}t        d      D ]  }|j                  |      } |S )	Nr   )_EKSBlowfishH   z6The password is too long. It must be 72 bytes at most.r      z-bcrypt cost factor must be in the range 4..31@   )Crypto.Cipherr   r   r    r   MODE_ECBrA   encrypt)	r"   costr#   constantinvertr   cipherctext_s	            r)   _bcrypt_hashr     s    *
8}rQRROOHII HIIh(=(=tT6RFE2Y &u%&Lr+   c                    t        | d      } | j                  t        d      d         dk7  rt        d      t	        |       dk  r| dz  } |t        d      }t	        |      dk7  rt        d	      t        | ||d
d      }dt        t        |      j                  d            z   }dt        |      z   }t        |dd       }d|z   |z   |z   S )a>  Hash a password into a key, using the OpenBSD bcrypt protocol.

    Args:
      password (byte string or string):
        The secret password or pass phrase.
        It must be at most 72 bytes long.
        It must not contain the zero byte.
        Unicode strings will be encoded as UTF-8.
      cost (integer):
        The exponential factor that makes it slower to compute the hash.
        It must be in the range 4 to 31.
        A value of at least 12 is recommended.
      salt (byte string):
        Optional. Random byte string to thwarts dictionary and rainbow table
        attacks. It must be 16 bytes long.
        If not passed, a random value is generated.

    Return (byte string):
        The bcrypt hash

    Raises:
        ValueError: if password is longer than 72 bytes or if it contains the zero byte

   zutf-8r   rw   z#The password contains the zero byter   rK   Nre   z!bcrypt salt must be 16 bytes longs   OrpheanBeholderScryDoubtT   $r9   s   $2a)r   r   r	   r    r   r   r   r
   strr   r   )r"   r   r#   r   cost_encsalt_enchash_encs          r)   bcryptr     s    4 x)H}}T!WQZ B&>??
8}rG|#
4yB<==4/JDQEd3t9??1-..HnT**HeCRj)HHx'(22r+   c                    t        |      }t        |      dk7  rt        dt        |      z        |dd dk7  rt        d      t        j                  d      }|j                  |      }|st        d      t        |j                  d	            }d|cxk  rd
k  st        d       t        d      t        |j                  d            }t        | ||      }t        d      }t        j                  d||      j                         }t        j                  d||      j                         }	||	k7  rt        d      y)a  Verify if the provided password matches the given bcrypt hash.

    Args:
      password (byte string or string):
        The secret password or pass phrase to test.
        It must be at most 72 bytes long.
        It must not contain the zero byte.
        Unicode strings will be encoded as UTF-8.
      bcrypt_hash (byte string, bytearray):
        The reference bcrypt hash the password needs to be checked against.

    Raises:
        ValueError: if the password does not match
    <   z;Incorrect length of the bcrypt hash: %d bytes instead of 60Nr   s   $2a$zUnsupported prefixs@   \$2a\$([0-9][0-9])\$([A-Za-z0-9./]{22,22})([A-Za-z0-9./]{31,31})zIncorrect bcrypt hash formatr   r   zIncorrect costr9   re      )digest_bitsrD   r   zIncorrect bcrypt hash)r   r   r    recompilematchr   groupr   r   r   r   r   r!   )
r"   bcrypt_hashr1   r   r   r#   bcrypt_hash2secretmac1mac2s
             r)   bcrypt_checkr   "  s*     +&K
;2VY\]hYiijj2A'!-..


WXA	A788qwwqz?DOO)** )**!''!*%D(D$/Lb!F;;3FELLND;;3FFMMODt|011 r+   c                    |d}|j                  d      dk7  rt        d      t        ||z  dz  d      }||z  }d}d}	t        |	      |k  rHt        |d      |z   dz   |z   |z   }
|	 || |
      z  }	|dz  }|d	kD  rt        d
      t        |	      |k  rH|dk(  r|	d| S t	        d||      D cg c]
  }|	|||z     }}|S c c}w )aJ  Derive one or more keys from a master secret using
    a pseudorandom function in Counter Mode, as specified in
    `NIST SP 800-108r1 <https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-108r1.pdf>`_.

    Args:
     master (byte string):
        The secret value used by the KDF to derive the other keys.
        It must not be a password.
        The length on the secret must be consistent with the input expected by
        the :data:`prf` function.
     key_len (integer):
        The length in bytes of each derived key.
     prf (function):
        A pseudorandom function that takes two byte strings as parameters:
        the secret and an input. It returns another byte string.
     num_keys (integer):
        The number of keys to derive. Every key is :data:`key_len` bytes long.
        By default, only 1 key is derived.
     label (byte string):
        Optional description of the purpose of the derived keys.
        It must not contain zero bytes.
     context (byte string):
        Optional information pertaining to
        the protocol that uses the keys, such as the identity of the
        participants, nonces, session IDs, etc.
        It must not contain zero bytes.

    Return:
        - a byte string (if ``num_keys`` is not specified), or
        - a tuple of byte strings (if ``num_key`` is specified).
    Nr   rK   rw   zNull byte found in contextr   r   r+   l    zOverflow in SP800 108 counterr   )r   r    r   r   r   )r   r   r7   r   labelr   key_len_encr   r(   r   r{   r   r   s                r)   SP800_108_Counterr   O  s   B ||G"566( 2Q 6:K8#J	A	B
b'J
Q"U*W4w>L
c&$	Qz><== b'J
 1}(7| %Q
G<> #cGm$ > >
>s   .C)  N)re   r   NN)r   N)r   r/   )Nr+   r+   )0r   r?   	functoolsr   Crypto.Util.py3compatr   r   r   r   r   r	   r
   Crypto.Hashr   r   r   r   r   Crypto.Util.strxorr   Crypto.Randomr   Crypto.Util.numberr   r   r   r   Crypto.Util._raw_apir   r   r   r   r   r   r*   rG   objectrI   ru   r   r   r   r   r   r   r   r   r   r6   r+   r)   <module>r      s   2 
  6 6 6 : 9 % * M M< < -,  ,-("VTn]6 ]@

/ dM`08 ,3^*2Z8r+   