o
    -׾g                     @   s(  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rNddlmZmZ n!edksVedkrgd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rdd Z dd Z!ndd Z dd Z!dddZ"dd Z#dS )    )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                  C   s$   t td} t| }t| t| S )NzLARGE_INTEGER *)r   r   QueryPerformanceCounterr   r   )numberres r   V/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/oscrypto/kdf.py
_get_start$   s   

r   c                 C   s   t  |  }t|d S )Ng     @@)r   int)startlengthr   r   r   _get_elapsed*   s   
r#   c                   C   s   t  S )N)r   nowr   r   r   r   r   /   s   c                 C   s:   t  |  }|j|jd d  }|jd }t||d  S )N   i  i  )r   r$   secondsdaysmicrosecondsr    )r!   r"   r&   millisecondsr   r   r   r#   2   s   
d   Fc                    s(   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 rGttdttj	rPt
tdd	d
dt fdd}| }t| d | }| dk rdnd}tt|}|dkr|d d }|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                     s>   t  } t  t| }std|f  d | S )Nz%s iterations in %smsg      ?)r   r   r#   print)r!   observed_mshash_algorithm
iterations
key_lengthpasswordquietsalt	target_msr   r   _measure   s   z-pbkdf2_iteration_calculator.<locals>._measureg       @i i N  i  )set
ValueErrorr   repr
isinstancer   	TypeErrorr
   r   pure_pythonOSErrorencoder	   r    round)r5   r7   r;   r9   r<   fractionround_factorresultr   r4   r   r   9   sT   



r   c                 C   s\  t |tsttdt|t |tsttdt|t |ts*ttdt||dk r7ttdt|t |tsEttdt||dk rRttdt|| tg dvrcttd	t| |d
krz| tddgv rzttd| t|f|dkr| dkrttdt|t	t
| }|||  }td|d D ]}|| }q|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
            r0   r1   )md2md5r+   zT
            hash_algorithm must be one of "md2", "md5", "sha1", not %s
               rK   rL   zM
            key_length can not be longer than 16 for %s - is %s
               r+   zO
            key_length can not be longer than 20 for sha1 - is %s
               N)rB   r   rC   r   r
   r   r@   rA   r?   getattrhashlibdigestrange)r5   r8   r:   r6   r7   algooutput_r   r   r   r      sd   





r   )r*   F)$
__future__r   r   r   r   sysrQ   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   r   <module>   s0   


g