
    Wwg                    
   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	ej$                  
      ZeZej+                  e	j,                  j&                          G d dej$                  
      ZeZej+                  e	j,                  j.                         e	j,                  j2                  Ze	j,                  j4                  Z	 d	 	 	 	 	 	 	 ddZddZddZddZddZddZ ddZ!dZ"ddZ#y)    )annotationsN)gcd)openssl)_serializationhashes)AsymmetricPadding)utilsc                     e Zd Zej                  dd       Zeej                  d	d              Zej                  d
d       Zej                  	 	 	 	 	 	 	 	 dd       Z	ej                  dd       Z
ej                  	 	 	 	 	 	 	 	 dd       Zy)RSAPrivateKeyc                     y)z3
        Decrypts the provided ciphertext.
        N )self
ciphertextpaddings      d/var/www/horilla/myenv/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pydecryptzRSAPrivateKey.decrypt           c                     yz7
        The bit length of the public modulus.
        Nr   r   s    r   key_sizezRSAPrivateKey.key_size   r   r   c                     y)zD
        The RSAPublicKey associated with this private key.
        Nr   r   s    r   
public_keyzRSAPrivateKey.public_key    r   r   c                     y)z!
        Signs the data.
        Nr   )r   datar   	algorithms       r   signzRSAPrivateKey.sign&   r   r   c                     y)z/
        Returns an RSAPrivateNumbers.
        Nr   r   s    r   private_numberszRSAPrivateKey.private_numbers1   r   r   c                     yz6
        Returns the key serialized as bytes.
        Nr   )r   encodingformatencryption_algorithms       r   private_byteszRSAPrivateKey.private_bytes7   r   r   N)r   bytesr   r   returnr'   r(   int)r(   RSAPublicKey)r   r'   r   r   r   +asym_utils.Prehashed | hashes.HashAlgorithmr(   r'   )r(   RSAPrivateNumbers)r#   _serialization.Encodingr$   z_serialization.PrivateFormatr%   z)_serialization.KeySerializationEncryptionr(   r'   )__name__
__module____qualname__abcabstractmethodr   propertyr   r   r   r    r&   r   r   r   r   r      s     
   
 	 
 	 # ?	
 
  	 
 	) - H	
 
 r   r   )	metaclassc                  P   e Zd Zej                  d	d       Zeej                  d
d              Zej                  dd       Zej                  	 	 	 	 	 	 dd       Z	ej                  	 	 	 	 	 	 	 	 	 	 dd       Z
ej                  	 	 	 	 	 	 	 	 dd       Zej                  dd       Zy)r+   c                     y)z/
        Encrypts the given plaintext.
        Nr   )r   	plaintextr   s      r   encryptzRSAPublicKey.encryptH   r   r   c                     yr   r   r   s    r   r   zRSAPublicKey.key_sizeN   r   r   c                     y)z-
        Returns an RSAPublicNumbers
        Nr   r   s    r   public_numberszRSAPublicKey.public_numbersU   r   r   c                     yr"   r   )r   r#   r$   s      r   public_byteszRSAPublicKey.public_bytes[   r   r   c                     y)z5
        Verifies the signature of the data.
        Nr   )r   	signaturer   r   r   s        r   verifyzRSAPublicKey.verifye   r   r   c                     y)z@
        Recovers the original data from the signature.
        Nr   )r   r@   r   r   s       r   recover_data_from_signaturez(RSAPublicKey.recover_data_from_signatureq   r   r   c                     y)z"
        Checks equality.
        Nr   )r   others     r   __eq__zRSAPublicKey.__eq__|   r   r   N)r8   r'   r   r   r(   r'   r)   )r(   RSAPublicNumbers)r#   r.   r$   z_serialization.PublicFormatr(   r'   )
r@   r'   r   r'   r   r   r   r,   r(   None)r@   r'   r   r   r   zhashes.HashAlgorithm | Noner(   r'   )rE   objectr(   bool)r/   r0   r1   r2   r3   r9   r4   r   r<   r>   rA   rC   rF   r   r   r   r+   r+   G   s0    
   
 	 
 	) , 
	  			 	 #		
 ?	 
	 	 	 # /	
 
  	 r   r+   c                Z    t        | |       t        j                  j                  | |      S N)_verify_rsa_parametersrust_opensslrsagenerate_private_key)public_exponentr   backends      r   rP   rP      s'    
 ?H500(KKr   c                B    | dvrt        d      |dk  rt        d      y )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)rQ   r   s     r   rM   rM      s6    j(?
 	

 $?@@ r   c                x    d\  }}| |}}|dkD  r(t        ||      \  }}|||z  z
  }||||f\  }}}}|dkD  r(||z  S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )   r   r   )divmod)	emx1x2abqrxns	            r   _modinvrb      sb     FBaqA
a%a|1!b&[!R|1b" a% 6Mr   c                    t        ||       S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rb   )pr_   s     r   rsa_crt_iqmpre      s     1a=r   c                    | |dz
  z  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rW   r   )private_exponentrd   s     r   rsa_crt_dmp1rh          
 q1u%%r   c                    | |dz
  z  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rW   r   )rg   r_   s     r   rsa_crt_dmq1rk      ri   r   c                V    |dz
  |dz
  z  t        |dz
  |dz
        z  }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.
    rW   )r   rb   )rY   rd   r_   lambda_ns       r   rsa_recover_private_exponentrn      s7    " A!a% CAq1u$55H1hr   i  c                   dt        d||z  |       k7  rt        d      ||z  dz
  }|}|dz  dk(  r|dz  }|dz  dk(  rd}d}|s|t        k  rxt        j                  d| dz
        }|dz  }|}||k  rGt        |||       }	|	dk7  r*|	| dz
  k7  r"t        |	d|       dk(  rt        |	dz   |       }
d}n|dz  }||k  rG|s
|t        k  rx|st        d      t        | 
      \  }}|dk(  sJ t        |
|f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 matchrW      r   FTz2Unable to compute factors p and q from exponent d.)reverse)powrU   _MAX_RECOVERY_ATTEMPTSrandomrandintr   rX   sorted)nrY   dktottspottedtriesr]   kcandrd   r_   r`   s                r   rsa_recover_prime_factorsr      sH    
SQUA.//q519D 	A
a%1*F a%1* GE%"88NN1a!e$
$hq!Q<DqyTa!e_T1aA1E q!$FA $h %"88 MNN!Q<DAq6M61a&$'DAqq6Mr   rL   )rQ   r*   r   r*   rR   z
typing.Anyr(   r   )rQ   r*   r   r*   r(   rH   )rY   r*   rZ   r*   r(   r*   )rd   r*   r_   r*   r(   r*   )rg   r*   rd   r*   r(   r*   )rg   r*   r_   r*   r(   r*   )rY   r*   rd   r*   r_   r*   r(   r*   )rx   r*   rY   r*   ry   r*   r(   ztuple[int, int])$
__future__r   r2   ru   typingmathr   "cryptography.hazmat.bindings._rustr   rN   cryptography.hazmat.primitivesr   r   *cryptography.hazmat.primitives._asymmetricr   )cryptography.hazmat.primitives.asymmetricr	   
asym_utilsABCMetar   RSAPrivateKeyWithSerializationregisterrO   r+   RSAPublicKeyWithSerializationr-   rG   rP   rM   rb   re   rh   rk   rn   rt   r   r   r   r   <module>r      s  
 # 
    F A H I.ckk .b "/    |''55 69S[[ 9x !-    l&&33 4 $$66 ##44  LLL L 	LA
&& .  +r   