
    j
il                     R    d dl Z d dlmZ d dlmZmZ d dlmZ  G d de          ZdS )    N)Any)CipherProtocolSerializerProtocol)JsonPlusSerializerc                       e Zd ZdZ e            fdededdfdZdede	e
ef         fdZd	e	e
ef         defd
Ze e            fdededd fd            ZdS )EncryptedSerializerzHSerializer that encrypts and decrypts data using an encryption protocol.cipherserdereturnNc                 "    || _         || _        d S )N)r	   r
   )selfr	   r
   s      C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langgraph/checkpoint/serde/encrypted.py__init__zEncryptedSerializer.__init__   s     


    objc                     | j                             |          \  }}| j                            |          \  }}| d| |fS )zESerialize an object to a tuple `(type, bytes)` and encrypt the bytes.+)r
   dumps_typedr	   encrypt)r   r   typdata
ciphername
ciphertexts         r   r   zEncryptedSerializer.dumps_typed   sP     J**3//	T!%!4!4T!:!:
J$$
$$j00r   r   c                     |\  }}d|vr| j                             |          S |                    dd          \  }}| j                            ||          }| j                             ||f          S )Nr      )r
   loads_typedsplitr	   decrypt)r   r   
enc_cipherr   r   r   decrypted_datas          r   r   zEncryptedSerializer.loads_typed   sx    !%
Jj  :))$///$**322Z,,ZDDz%%sN&;<<<r   kwargsc                    	 ddl m n# t          $ r t          d          dw xY wdv r                    d          nYt	          j        d          }|t          d          |                                t                    dvrt          d	          	                    d
          
j
        d
<    G fddt                    } |  |            |          S )z5Create an `EncryptedSerializer` using AES encryption.r   )AESzQPycryptodome is not installed. Please install it with `pip install pycryptodome`.NkeyLANGGRAPH_AES_KEYz2LANGGRAPH_AES_KEY environment variable is not set.)          z3LANGGRAPH_AES_KEY must be 16, 24, or 32 bytes long.modec                   T    e Zd Zdedeeef         f fdZdededef fdZdS )HEncryptedSerializer.from_pycryptodome_aes.<locals>.PycryptodomeAesCipher	plaintextr   c                 n     j         fi }|                    |          \  }}d|j        |z   |z   fS )Naes)newencrypt_and_digestnonce)r   r,   r	   r   tagr#   r$   r!   s        r   r   zPEncryptedSerializer.from_pycryptodome_aes.<locals>.PycryptodomeAesCipher.encryptB   sJ     ////"(";";I"F"F
CflS0:===r   r   r   c                     |dk    sJ d|             |d d         }|dd         }|dd          } j         fi 	d|i}|                    ||          S )Nr.   zUnsupported cipher: r&   r(   r1   )r/   decrypt_and_verify)
r   r   r   r1   r2   actual_ciphertextr	   r#   r$   r!   s
          r   r   zPEncryptedSerializer.from_pycryptodome_aes.<locals>.PycryptodomeAesCipher.decryptG   s    !U***,O:,O,O***"3B3 B'$.rssO! <<<<e<<<001BCHHHr   N)__name__
__module____qualname__bytestuplestrr   r   )r#   r$   r!   s   r   PycryptodomeAesCipherr+   A   s        > >5e3D > > > > > > > >
I# I5 IU I I I I I I I I I Ir   r<   )Crypto.Cipherr#   ImportErrorpoposgetenv
ValueErrorencodelengetMODE_EAXr   )clsr
   r!   key_strr<   r#   r$   s     `  @@r   from_pycryptodome_aesz)EncryptedSerializer.from_pycryptodome_aes&   sJ   
	))))))) 	 	 	c 	 F??E**CCi 344G !UVVV..""C3xx|++ !VWWW ::f% \F6N	I 	I 	I 	I 	I 	I 	I 	I 	IN 	I 	I 	I s((**E222s    ')r6   r7   r8   __doc__r   r   r   r   r   r:   r;   r9   r   r   classmethodrI    r   r   r   r      s        RR CUBTBVBV $-?	   1s 1uS%Z'8 1 1 1 1
=c5j 1 
=c 
= 
= 
= 
= ););)=)=)3 )3&)3IL)3	)3 )3 )3 [)3 )3 )3r   r   )	r@   typingr   langgraph.checkpoint.serde.baser   r   #langgraph.checkpoint.serde.jsonplusr   r   rL   r   r   <module>rP      s    				       N N N N N N N N B B B B B BH3 H3 H3 H3 H3, H3 H3 H3 H3 H3r   