o
    j(                     @   sn   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ G dd	 d	ZdS )
z*Amazon SiegeCrypto client-side encryption.    N)AESGCM)OAEPMGF1)RSAPublicNumbers)SHA1)default_backendc                	   @   s   e Zd ZdZdZdZddedededd	fd
dZdedefddZ	dede
fddZde
de
de
fddZde
de
fddZde
fddZd	S )CseAmazonSxgitario         si:md5jwk_nkey_idprovider_idreturnNc                 C   s   || _ || _|| _d S )N)_CseAmazonSxgitario__jwkN_CseAmazonSxgitario__keyId_CseAmazonSxgitario__providerId)selfr   r   r    r   6/Users/user/Downloads/amz/amazon/cseAmazonSxgitario.py__init__   s   
zCseAmazonSxgitario.__init__passwordc              
   C   sp   z|  |}|  |}d|t|dt|ddW S  ty7 } zdt|dW  Y d }~S d }~ww )NTutf-8)statuspayloadencryptedPasswordencryptedPasswordCheckF)r   description)#_CseAmazonSxgitario__encryptMessagebase64	b64encodedecode	Exceptionstr)r   r   Zenc1Zenc2errorr   r   r   encrypt   s   

zCseAmazonSxgitario.encrypt	plaintextc           	      C   s`   t d}t d}t d}| |}t|}|||d|}| ||}| ||||S )N      r   )osurandom_CseAmazonSxgitario__buildAADr   r&   encode_CseAmazonSxgitario__wrapKey_CseAmazonSxgitario__serialize)	r   r'   
message_iddata_keyivaadZaesgcm
ciphertextencrypted_data_keyr   r   r   Z__encryptMessage#   s   



z#CseAmazonSxgitario.__encryptMessager1   r0   c                 C   sF   t tddt| jddt }||| ttt dt d dS )NZAQABr(   $   )en)	algorithm)Zmgfr9   label)	r   intr   
public_keyr   r&   r   r   r   )r   r1   r0   r<   r   r   r   Z	__wrapKey/   s   
zCseAmazonSxgitario.__wrapKeyc                 C   sP   | j d}| jd}tdt|| }|tdt|| 7 }||7 }|S )Nr   >H)r   r-   r   structpacklen)r   r0   providerZkey_id_bytesr3   r   r   r   Z
__buildAAD9   s   zCseAmazonSxgitario.__buildAADc           
      C   sz   t d| j| j| j}t dt|}t dt|}t dt|}t dt|}	|| | | | | | |	 | S )Nz>HBHr=   z>Bz>I)r>   r?   !_CseAmazonSxgitario__ALGORITHM_ID_CseAmazonSxgitario__VERSION_CseAmazonSxgitario__TYPEr@   )
r   r0   r5   r2   r4   headerZ
msg_id_lenZedk_lenZiv_lenZct_lenr   r   r   Z__serializeA   s   $zCseAmazonSxgitario.__serialize)r   )__name__
__module____qualname__rB   rC   rD   r$   r   dictr&   bytesr   r.   r,   r/   r   r   r   r   r      s    
r   )__doc__r    r*   r>   Z+cryptography.hazmat.primitives.ciphers.aeadr   Z1cryptography.hazmat.primitives.asymmetric.paddingr   r   Z-cryptography.hazmat.primitives.asymmetric.rsar   Z%cryptography.hazmat.primitives.hashesr   Zcryptography.hazmat.backendsr   r   r   r   r   r   <module>   s    