o
    Vh                     @  s  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ	 d dl
mZmZ d dlmZ d dlmZ G dd	 d	ejd
ZeZee	jj G dd dejd
ZeZee	jj e	jjZe	jjZ	d-d.ddZd/ddZd0ddZd1ddZd2d!d"Zd3d#d$Z d4d%d&Z!d'Z"d5d+d,Z#dS )6    )annotationsN)gcd)openssl)_serializationhashes)AsymmetricPadding)utilsc                   @  sp   e Zd ZejdddZeejd d	d
Zejd!ddZejd"ddZ	ejd#ddZ
ejd$ddZdS )%RSAPrivateKey
ciphertextbytespaddingr   returnc                 C     dS )z3
        Decrypts the provided ciphertext.
        N )selfr
   r   r   r   /var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pydecrypt       zRSAPrivateKey.decryptintc                 C  r   z7
        The bit length of the public modulus.
        Nr   r   r   r   r   key_size   r   zRSAPrivateKey.key_sizeRSAPublicKeyc                 C  r   )zD
        The RSAPublicKey associated with this private key.
        Nr   r   r   r   r   
public_key    r   zRSAPrivateKey.public_keydata	algorithm+asym_utils.Prehashed | hashes.HashAlgorithmc                 C  r   )z!
        Signs the data.
        Nr   )r   r   r   r   r   r   r   sign&   r   zRSAPrivateKey.signRSAPrivateNumbersc                 C  r   )z/
        Returns an RSAPrivateNumbers.
        Nr   r   r   r   r   private_numbers1   r   zRSAPrivateKey.private_numbersencoding_serialization.Encodingformat_serialization.PrivateFormatencryption_algorithm)_serialization.KeySerializationEncryptionc                 C  r   z6
        Returns the key serialized as bytes.
        Nr   )r   r    r"   r$   r   r   r   private_bytes7   r   zRSAPrivateKey.private_bytesN)r
   r   r   r   r   r   r   r   )r   r   )r   r   r   r   r   r   r   r   )r   r   )r    r!   r"   r#   r$   r%   r   r   )__name__
__module____qualname__abcabstractmethodr   propertyr   r   r   r   r'   r   r   r   r   r	      s    
r	   )	metaclassc                   @  s   e Zd Zejd$ddZeejd%d	d
Zejd&ddZejd'ddZ	ejd(ddZ
ejd)ddZejd*d!d"Zd#S )+r   	plaintextr   r   r   r   c                 C  r   )z/
        Encrypts the given plaintext.
        Nr   )r   r0   r   r   r   r   encryptH   r   zRSAPublicKey.encryptr   c                 C  r   r   r   r   r   r   r   r   N   r   zRSAPublicKey.key_sizeRSAPublicNumbersc                 C  r   )z-
        Returns an RSAPublicNumbers
        Nr   r   r   r   r   public_numbersU   r   zRSAPublicKey.public_numbersr    r!   r"   _serialization.PublicFormatc                 C  r   r&   r   )r   r    r"   r   r   r   public_bytes[   r   zRSAPublicKey.public_bytes	signaturer   r   r   Nonec                 C  r   )z5
        Verifies the signature of the data.
        Nr   )r   r6   r   r   r   r   r   r   verifye   r   zRSAPublicKey.verifyhashes.HashAlgorithm | Nonec                 C  r   )z@
        Recovers the original data from the signature.
        Nr   )r   r6   r   r   r   r   r   recover_data_from_signatureq   r   z(RSAPublicKey.recover_data_from_signatureotherobjectboolc                 C  r   )z"
        Checks equality.
        Nr   )r   r;   r   r   r   __eq__|   r   zRSAPublicKey.__eq__N)r0   r   r   r   r   r   r(   )r   r2   )r    r!   r"   r4   r   r   )
r6   r   r   r   r   r   r   r   r   r7   )r6   r   r   r   r   r9   r   r   )r;   r<   r   r=   )r)   r*   r+   r,   r-   r1   r.   r   r3   r5   r8   r:   r>   r   r   r   r   r   G   s     	
r   public_exponentr   r   backend
typing.Anyr   c                 C  s   t | | tj| |S N)_verify_rsa_parametersrust_opensslrsagenerate_private_key)r?   r   r@   r   r   r   rF      s   
rF   r7   c                 C  s$   | dvrt d|dk rt dd S )N)   i  zopublic_exponent must be either 3 (for legacy compatibility) or 65537. Almost everyone should choose 65537 here!i   z$key_size must be at least 1024-bits.)
ValueError)r?   r   r   r   r   rC      s   rC   emc           	      C  sX   d\}}| |}}|dkr(t ||\}}|||  }||||f\}}}}|dks|| S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )   r   r   )divmod)	rI   rJ   x1x2abqrxnr   r   r   _modinv   s   
rT   prQ   c                 C  s
   t || S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rT   )rU   rQ   r   r   r   rsa_crt_iqmp   s   
rV   private_exponentc                 C     | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rK   r   )rW   rU   r   r   r   rsa_crt_dmp1      rY   c                 C  rX   )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rK   r   )rW   rQ   r   r   r   rsa_crt_dmq1   rZ   r[   c                 C  s,   |d |d  t |d |d  }t| |S )z
    Compute the RSA private_exponent (d) given the public exponent (e)
    and the RSA primes p and q.

    This uses the Carmichael totient function to generate the
    smallest possible working value of the private exponent.
    rK   )r   rT   )rI   rU   rQ   lambda_nr   r   r   rsa_recover_private_exponent   s   "
r]   i  ndtuple[int, int]c                 C  s&  dt d|| | krtd|| d }|}|d dkr&|d }|d dksd}d}|sr|tk rrtd| d }|d7 }|}||k rlt ||| }	|	dkrd|	| d krdt |	d| dkrdt|	d | }
d}n|d9 }||k sB|sr|tk s0|sxtdt| |
\}}|dksJ t|
|fdd	\}
}|
|fS )
z
    Compute factors p and q from the private exponent d. We assume that n has
    no more than two factors. This function is adapted from code in PyCrypto.
       zn, d, e don't matchrK      r   FTz2Unable to compute factors p and q from exponent d.)reverse)powrH   _MAX_RECOVERY_ATTEMPTSrandomrandintr   rL   sorted)r^   rI   r_   ktottspottedtriesrO   kcandrU   rQ   rR   r   r   r   rsa_recover_prime_factors   s8   $ro   rB   )r?   r   r   r   r@   rA   r   r	   )r?   r   r   r   r   r7   )rI   r   rJ   r   r   r   )rU   r   rQ   r   r   r   )rW   r   rU   r   r   r   )rW   r   rQ   r   r   r   )rI   r   rU   r   rQ   r   r   r   )r^   r   rI   r   r_   r   r   r`   )$
__future__r   r,   rf   typingmathr   "cryptography.hazmat.bindings._rustr   rD   cryptography.hazmat.primitivesr   r   *cryptography.hazmat.primitives._asymmetricr   )cryptography.hazmat.primitives.asymmetricr   
asym_utilsABCMetar	   RSAPrivateKeyWithSerializationregisterrE   r   RSAPublicKeyWithSerializationr   r2   rF   rC   rT   rV   rY   r[   r]   re   ro   r   r   r   r   <module>   s6   1<
	




