HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/oscrypto/_mac/__pycache__/util.cpython-39.pyc
a

�,�h,'�@sddlmZmZmZmZddlZddlmZddlm	Z	m
Z
mZmZddl
mZmZmZmZddlmZdd	lmZmZdd
lmZgd�ZdZdd
gZdd�Zdd�Zdd�Zde_dd�Z z ddl!m"Z"dd�Z#dd�Z$Wney�ddl%m$Z$Yn0dS)�)�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Csbzt|t�WStyTtD]0}zt||dd�WYStyLYq0qYn0t|dd�S)N�strict)�errors�replace)r
�	_encoding�UnicodeDecodeError�_fallback_encodings)�value�encoding�r"�C/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/oscrypto/_mac/util.py�_try_decodesr$cCsFt�}zt�|�}Wnty.t|�YS0t|t�r>|St|�S)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_stringr"r"r#�_extract_error*s
r+cCst|t�sttdt|����t|t�s8ttdt|����t|t�sTttdt|����|dkrdtd��t|t�s�ttdt|����|dkr�td��|tgd��vr�ttd	t|����t	j
t	jt	jt	j
t	jd�|}t|�}t�t	j|t|�|t|�||||�	}|d
k�rtt���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
            rz!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�	TypeErrorrrrr'�set�reprrZkCCPRFHmacAlgSHA1ZkCCPRFHmacAlgSHA224ZkCCPRFHmacAlgSHA256ZkCCPRFHmacAlgSHA384ZkCCPRFHmacAlgSHA512rrZCCKeyDerivationPBKDFZ	kCCPBKDF2�len�OSErrorr+r	)�hash_algorithm�password�salt�
iterations�
key_length�algo�
output_buffer�resultr"r"r#r?sh
�
�
�
��	���

rFcCsnt|t�sttdt|����|dkr,td��|dkr<td��t|�}t�tj	||�}|dkrft
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
            rzlength must be greater than 0iz$length must not be greater than 1024r)
r(rr1rrr'rrZSecRandomCopyBytesZkSecRandomDefaultr5r+r	)�length�bufferr=r"r"r#r�s
�
r)�	libcryptocCs,t��}td�}t�||�t|�}t|�S)z�
        Extracts the last OpenSSL error message into a python unicode string

        :return:
            A unicode string error message
        �x)r@Z
ERR_get_errorrZERR_error_stringrr$)r)r?r*r"r"r#�_extract_openssl_error�s
rBc
Csft|t�sttdt|����t|t�s8ttdt|����t|t�sTttdt|����|dkrnttdt|����t|t�s�ttdt|����|dkr�ttdt|����|tgd��vr�ttd	t|����|tgd
��vr�ttdt|����|�	d��
d
�d}tjtj
tjtjtjtjd�|�}t|�}t�|t|�|t|�|||||�	}	|	dk�r^tt���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
                rzK
                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,r-r.r/r0z�
                hash_algorithm must be one of "md5", "sha1", "sha224", "sha256",
                "sha384", "sha512", not %s
                )rr�zD
                id_ must be one of 1, 2, 3, not %s
                rzutf-16bes)r(rr1rrrr'r3r2�decode�encoder@ZEVP_md5ZEVP_sha1Z
EVP_sha224Z
EVP_sha256Z
EVP_sha384Z
EVP_sha512rZPKCS12_key_gen_unir4r5rBr	)
r6r7r8r9r:Zid_Zutf16_passwordZdigest_typer<r=r"r"r#r�s�
�
�
��
������	�

r)r)&�
__future__rrrrr%�_errorsrZ_ffirr	r
r�_typesrr
rrrrZ_common_cryptorrZ	_securityr�__all__rrr$r+rZpure_pythonrZ_openssl._libcryptor@rBrZ_pkcs12r"r"r"r#�<module>s*e(|