
    9i|                     J    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y)    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y)EncryptedSerializerzHSerializer that encrypts and decrypts data using an encryption protocol.cipherserdereturnNc                      || _         || _        y )N)r	   r
   )selfr	   r
   s      n/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/langgraph/checkpoint/serde/encrypted.py__init__zEncryptedSerializer.__init__   s     
    objc                     | j                   j                  |      \  }}| 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   sK     JJ**3/	T!%!4!4T!:
Ja
|$j00r   r   c                     |\  }}d|vr| j                   j                  |      S |j                  dd      \  }}| j                  j	                  ||      }| j                   j                  ||f      S )Nr      )r
   loads_typedsplitr	   decrypt)r   r   
enc_cipherr   r   r   decrypted_datas          r   r   zEncryptedSerializer.loads_typed   so    !%
Jj ::))$//$**32Z,,ZDzz%%sN&;<<r   kwargsc                    	 ddl m dv rj                  d      nJt	        j
                  d      }|t        d      |j                         t              dvrt        d	      j                  d
      j                  d
<    G fddt              } |  |       |      S # t        $ r t        d      dw xY w)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                   L    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y)HEncryptedSerializer.from_pycryptodome_aes.<locals>.PycryptodomeAesCipher	plaintextr   c                 z     j                   fi }|j                  |      \  }}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   sE     //"(";";I"F
CfllS0:===r   r   r   c                     |dk(  s
J d|        |d d }|dd }|dd  } j                   fi 	d|i}|j                  ||      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   so    !U*O.B:,,OO*"3B B'$.rsO! <<e<001BCHHr   N)__name__
__module____qualname__bytestuplestrr   r   )r#   r$   r!   s   r   PycryptodomeAesCipherr+   A   sC    > >5e3D >
I# I5 IU 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&   s    
	) F?E*Cii 34G !UVV.."C3x|+ !VWW ::f% \\F6N	I 	IN 	I (*E22G  	c	s   B, ,C)r6   r7   r8   __doc__r   r   r   r   r   r:   r;   r9   r   r   classmethodrI    r   r   r   r      s    R CUBV$-?	1s 1uS%Z'8 1
=c5j 1 
=c 
= );)=)3&)3IL)3	)3 )3r   r   )	r@   typingr   langgraph.checkpoint.serde.baser   r   #langgraph.checkpoint.serde.jsonplusr   r   rL   r   r   <module>rP      s     	  N BH3, H3r   