
    Bwg,'                         d dl mZmZmZmZ d dlZddlmZ ddlm	Z	m
Z
mZmZ ddlmZmZmZmZ ddlmZ dd	lmZmZ dd
lmZ g dZdZddgZd Zd Zd Zde_        d Z 	 ddl!m"Z" d Z#d Z$y# e$ r	 ddl%m$Z$ Y yw xY w)    )unicode_literalsdivisionabsolute_importprint_functionN   )pretty_message)buffer_from_bytesbytes_from_buffererrnobyte_string_from_buffer)	type_namestr_clsbyte_cls	int_types)LibraryNotFoundError   )CommonCryptoCommonCryptoConst)Security)pbkdf2
pkcs12_kdf
rand_bytesutf-8cp1252c                     	 t        | t              S # t        $ r/ t        D ]#  }	 t        | |d      c cY S # t        $ r Y !w xY w Y nw xY wt        | d      S )Nstrict)errorsreplace)r   	_encodingUnicodeDecodeError_fallback_encodings)valueencodings     I/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_mac/util.py_try_decoder%      si    
ui((  + 	Huhx@@& 	 5++s/    A
7A
	A A
AA
	A
c                      t               } 	 t        j                  |       }t        |t              r|S t        |      S # t        $ r t	        |       cY S w xY w)z~
    Extracts the last OS error message into a python unicode string

    :return:
        A unicode string error message
    )r   osstrerror
ValueErrorr   
isinstancer%   )	error_numerror_strings     r$   _extract_errorr-   *   sW     I"{{9- ,(|$$  "y!!"s   > AA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        d      t        |t
              st        t        dt	        |                  |dk  rt        d      | t        g d      vrt        t        d	t        |                   t        j                  t        j                  t        j                  t        j                  t        j                  d|    }t        |      }t!        j"                  t        j$                  |t'        |      |t'        |      ||||	      }|d
k7  rt)        t+                     t-        |      S )a  
    PBKDF2 from PKCS#5

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

    :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

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :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   z!iterations must be greater than 0z?
            key_length must be an integer, not %s
            z!key_length must be greater than 0)sha1sha224sha256sha384sha512zz
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            r   )r*   r   	TypeErrorr   r   r   r)   setreprr   kCCPRFHmacAlgSHA1kCCPRFHmacAlgSHA224kCCPRFHmacAlgSHA256kCCPRFHmacAlgSHA384kCCPRFHmacAlgSHA512r	   r   CCKeyDerivationPBKDF	kCCPBKDF2lenOSErrorr-   r
   )hash_algorithmpasswordsalt
iterations
key_lengthalgooutput_bufferresults           r$   r   r   ?   s   8 h) h	
  	 dH% dO	
  	 j), j!	
  	 A~<==j), j!	
  	 A~<==S!QRR  
  	 "33#77#77#77#77 D &j1M..##HD	
F {n&'']++    Fc                 D   t        | t              st        t        dt	        |                   | dk  rt        d      | dkD  rt        d      t        |       }t        j                  t        j                  | |      }|dk7  rt        t                     t        |      S )a  
    Returns a number of random bytes suitable for cryptographic purposes

    :param length:
        The desired number of bytes

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string
    z;
            length must be an integer, not %s
            r   zlength must be greater than 0i   z$length must not be greater than 1024r   )r*   r   r4   r   r   r)   r	   r   SecRandomCopyByteskSecRandomDefaultr?   r-   r
   )lengthbufferrG   s      r$   r   r      s      fi( f	
  	 z899}?@@v&F(()C)CVVTF{n&''V$$rH   )	libcryptoc                      t        j                         } t        d      }t        j                  | |       t	        |      }t        |      S )z
        Extracts the last OpenSSL error message into a python unicode string

        :return:
            A unicode string error message
        x   )rN   ERR_get_errorr	   ERR_error_stringr   r%   )r+   rM   r,   s      r$   _extract_openssl_errorrS      sC     ++-	"3'""9f5 /v6<((rH   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        |                   |t        g d
      vrt        t        dt        |                  |j                  d      j                  d      dz   }	 t        j                  t        j                  t        j                  t        j                  t        j                   t        j"                  d|           }t%        |      }t        j&                  |t)        |      |t)        |      |||||	      }	|	dk7  rt+        t-                     t/        |      S )a  
        KDF from RFC7292 appendix B.2 - https://tools.ietf.org/html/rfc7292#page-19

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

        :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

        :param id_:
            The ID of the usage - 1 for key, 2 for iv, 3 for mac

        :raises:
            ValueError - when any of the parameters contain an invalid value
            TypeError - when any of the parameters are of the wrong type
            OSError - when an error is returned by the OS crypto library

        :return:
            The derived key as a byte string
        zH
                password must be a byte string, not %s
                zD
                salt must be a byte string, not %s
                zG
                iterations must be an integer, not %s
                r   zK
                iterations must be greater than 0 - is %s
                zG
                key_length must be an integer, not %s
                zK
                key_length must be greater than 0 - is %s
                )md5r/   r0   r1   r2   r3   z
                hash_algorithm must be one of "md5", "sha1", "sha224", "sha256",
                "sha384", "sha512", not %s
                )r   r      zD
                id_ must be one of 1, 2, 3, not %s
                r   zutf-16bes     )r*   r   r4   r   r   r   r)   r6   r5   decodeencoderN   EVP_md5EVP_sha1
EVP_sha224
EVP_sha256
EVP_sha384
EVP_sha512r	   PKCS12_key_gen_unir>   r?   rS   r
   )
r@   rA   rB   rC   rD   id_utf16_passworddigest_typerF   rG   s
             r$   r   r      sL   > (H-N (#	   $)N $	   *i0N *%	   >^ Z 	   *i0N *%	   >^ Z 	   %\!]]^ ^$   c)n$^ S		   "188D{R$$&&********
   **5--I

 Q;0233 //rH   )r   )&
__future__r   r   r   r   r'   _errorsr   _ffir	   r
   r   r   _typesr   r   r   r   r   r   _common_cryptor   r   	_securityr   __all__r   r!   r%   r-   r   pure_pythonr   _openssl._libcryptorN   rS   r   _pkcs12 rH   r$   <module>rn      s    R R 	 $ W W < < ) ;  	) ,"%*b,J  #%PR%/)"z0x 	 %$%s   !A. .A<;A<