
    Bwg                     >   d dl mZmZmZm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mZmZ ddlmZ dd	lmZmZ  e	       Zed
k(  r	ddlmZmZ n#edk(  sedk(  rddlmZmZ ddlmZmZ nddlmZmZ g dZej>                  dk(  rd Z d Z!nd Z d Z!ddZ"d Z#y)    )unicode_literalsdivisionabsolute_importprint_functionN)datetime   )backend)
rand_bytes)	type_namebyte_cls	int_types)pretty_message)newderefmac)pbkdf2
pkcs12_kdfwin	winlegacy)kernel32handle_error)pbkdf1r   pbkdf2_iteration_calculatorr   win32c                  x    t        t        d      } t        j                  |       }t        |       t	        |       S )NzLARGE_INTEGER *)r   r   QueryPerformanceCounterr   r   )numberress     C/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/kdf.py
_get_startr    $   s1    X01..v6SV}    c                 8    t               | z
  }t        |dz        S )Ng     @@)r    int)startlengths     r   _get_elapsedr&   *   s    %6F?##r!   c                  *    t        j                         S )N)r   now r!   r   r    r    /   s    ||~r!   c                     t        j                         | z
  }|j                  |j                  dz  dz  z   }|j                  dz  }t        ||dz  z         S )N   i  i  )r   r(   secondsdaysmicrosecondsr#   )r$   r%   r,   millisecondss       r   r&   r&   2   sS    %'..FKK"$4t$;<++g5<7W#4566r!   c                    	
  t        g d      vrt        t        dt                           t	        t
              st        t        dt                          dk  rt        t        dt                          t	        t
              st        t        dt                          dk  rt        t        dt                          t        j                  rt        t        d            d	d
j                  d      	t              
 	
fd} |       }t        |z  dz         |       }|z  dk  rdnd}t        t        |            }|dkD  r|dz  dz  }|S )a  
    Runs pbkdf2() twice to determine the approximate number of iterations to
    use to hit a desired time per run. Use this on a production machine to
    dynamically adjust the number of iterations as high as you can.

    :param hash_algorithm:
        The string name of the hash algorithm to use: "md5", "sha1", "sha224",
        "sha256", "sha384", "sha512"

    :param key_length:
        The length of the desired key in bytes

    :param target_ms:
        The number of milliseconds the derivation should take

    :param quiet:
        If no output should be printed as attempts are made

    :return:
        An integer number of iterations of PBKDF2 using the specified hash
        that will take at least target_ms
    )sha1sha224sha256sha384sha512zz
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            ?
            key_length must be an integer, not %s
            r   C
            key_length must be greater than 0 - is %s
            z>
            target_ms must be an integer, not %s
            zB
            target_ms must be greater than 0 - is %s
            z
            Only a very slow, pure-python version of PBKDF2 is available,
            making this function useless
            i'  zthis is a testzutf-8c                      t               } t               t        |       }st        d|d       dz  |z  S )Nz iterations in msg      ?)r    r   r&   print)	r$   observed_mshash_algorithm
iterations
key_lengthpasswordquietsalt	target_mss	     r   _measurez-pbkdf2_iteration_calculator.<locals>._measure   sC    ~xz:F"5)ZEFY,,r!   g       @i i N  i  )set
ValueErrorr   repr
isinstancer   	TypeErrorr   r   pure_pythonOSErrorencoder
   r#   round)r<   r>   rB   r@   rC   fractionround_factorresultr=   r?   rA   s   ````    @@@r   r   r   9   s   0 S!QRR  
  	 j), j!	
  	 A~ 	
  	 i+ i 	
  	 1} O	
  	 n
  	 J&&w/Hj!D- - zHZ(*S01JzHh&J
 $f,2"Lz<01F~D.D(Mr!   c           	         t        |t              st        t        dt	        |                  t        |t              st        t        dt	        |                  t        |t
              st        t        dt	        |                  |dk  rt        t        dt        |                  t        |t
              st        t        dt	        |                  |dk  rt        t        dt        |                  | t        g d      vrt        t        d	t        |                   |d
kD  r/| t        ddg      v r t        t        d| t        |      f            |dkD  r#| dk(  rt        t        dt        |                  t        t        |       } |||z         j                         }t        d|dz         D ]  } ||      j                         } |d| S )aF  
    An implementation of PBKDF1 - should only be used for interop with legacy
    systems, not new architectures

    :param hash_algorithm:
        The string name of the hash algorithm to use: "md2", "md5", "sha1"

    :param password:
        A byte string of the password to use an input to the KDF

    :param salt:
        A cryptographic random byte string

    :param iterations:
        The numbers of iterations to use when deriving the key

    :param key_length:
        The length of the desired key in bytes

    :return:
        The derived key as a byte string
    z@
            password must be a byte string, not %s
            z<
            salt must be a byte string, not %s
            z?
            iterations must be an integer, not %s
            r   zC
            iterations must be greater than 0 - is %s
            r6   r7   )md2md5r1   zT
            hash_algorithm must be one of "md2", "md5", "sha1", not %s
               rS   rT   zM
            key_length can not be longer than 16 for %s - is %s
               r1   zO
            key_length can not be longer than 20 for sha1 - is %s
               N)rI   r   rJ   r   r   r   rG   rH   rF   getattrhashlibdigestrange)r<   r?   rA   r=   r>   algooutput_s           r   r   r      s   0 h) x 	
  	 dH% t_	
  	 j), z"	
  	 A~ 	
  	 j), z"	
  	 A~ 	
  	 S!788  	
  	 B>S%-@@ T*-.	
  	 B>V3 	
  	 7N+D(T/"))+F1j1n% 'f$$&' +:r!   )d   F)$
__future__r   r   r   r   sysrY   r    r	   utilr
   _typesr   r   r   _errorsr   _ffir   r   _backend	_mac.utilr   r   	_win.util_win._kernel32r   r   _openssl.util__all__platformr    r&   r   r   r)   r!   r   <module>rn      s    R R 
     2 2 #  9 u--(k1-661 <<7$
7dNer!   