o
    jn                     @   sj   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 )	    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d Zdede
fddZdedede
de
fddZde
fddZdede
fddZdede
fdd Zd	S )"	CseAmazon         si:md5jwk_nkey_idprovider_idreturnNc                 C   s   || _ || _|| _d S )N)_CseAmazon__jwkN_CseAmazon__keyId_CseAmazon__providerId)selfr   r   r    r   '/Users/user/Downloads/amz/amazon/cse.py__init__   s   
zCseAmazon.__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)_CseAmazon__encryptMessagebase64	b64encodedecode	Exceptionstr)r   r   Zenc1Zenc2errorr   r   r   encrypt   s   

zCseAmazon.encryptsc                 C   s"   t |ddt|d  d   S )N=   )r    urlsafe_b64decodelen)r   r'   r   r   r   Z__b64UrlDecode!   s   "zCseAmazon.__b64UrlDecodec                 C   s8   t | | jd}t | dd}t||t S )NbigZAQAB)int
from_bytes_CseAmazon__b64UrlDecoder   r   
public_keyr   )r   ner   r   r   Z__importPublicKey$   s   zCseAmazon.__importPublicKeycontextc                 C   s   |st ddS t| }t dt|}|D ]%\}}|d|d}}|t dt|| t dt| | 7 }q|S )N>Hr   r   )structpacksorteditemsr+   encode)r   r3   r8   resultkeyvaluekbZvbr   r   r   Z__serializeEncryptionContext)   s   .z&CseAmazon.__serializeEncryptionContextencrypted_keyc                 C   sP   | d}| d}tdt|| tdt| | tdt| | S )Nr   r4   )r9   r5   r6   r+   )r   r   r   r>   pbr=   r   r   r   Z__serializeEncryptedDataKey3   s   

z%CseAmazon.__serializeEncryptedDataKeyc                 C   s   t dS )N   )bytes)r   r   r   r   Z__buildHeaderIv<   s   zCseAmazon.__buildHeaderIvseqc                 C   s   t ddt d| S )Nz>Qr   >I)r5   r6   )r   rB   r   r   r   Z__buildFrameIv?   s   zCseAmazon.__buildFrameIvc                 C   s  t d}t d}|  }||ttt dt d d}| | j| j	|}t
 }|| j || j |td| j || || i  |tdd || |d |tdd |d	 |tdt|d  t|}t|}	|  }
|	|
d
|}|d}| d}tdd}tdd}tdt|}|	||d
}||
 | | | | | | S )N   )	algorithm)ZmgfrE   labelr4   r
      rC   r   r@       r   l    )osurandom_CseAmazon__importPublicKeyr&   r   r   r   %_CseAmazon__serializeEncryptedDataKeyr   r   	bytearrayappend_CseAmazon__VERSION_CseAmazon__TYPEextendr5   r6   _CseAmazon__ALGORITHM_ID&_CseAmazon__serializeEncryptionContextr+   rA   r   _CseAmazon__buildHeaderIvr9   _CseAmazon__buildFrameIv)r   r   Z
message_idZdata_keyr0   Zencrypted_data_keyZedk_serializedheaderZ
raw_headerZaesgcmZ	header_ivZheader_auth_tagZpassword_bytesZframe_ivZfinal_frame_markerZseq_num_bytesZcontent_len_bytesZencrypted_contentr   r   r   Z__encryptMessageB   s`   








zCseAmazon.__encryptMessage)r   )__name__
__module____qualname__rR   rO   rP   r$   r   dictr&   rA   r/   rK   rS   rL   rT   r-   rU   r   r   r   r   r   r   	   s    
	r   )r    rI   r5   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    