
    k7i                         S SK r S SKrS SKJrJr  S SKJrJr  S SKJ	r	  S SK
JrJrJr  S SKJr  S SKJr  S SKJr  S	rS
r\\/\4   r\\\/\4   r " S S\5      rg)    N)CallableSequence)AnySupportsFloat)SerializationError)CorruptedDataErrorDecryptionErrorEncryptionError)override)AsyncKeyValue)BaseWrapper__encrypted_data____encryption_version__c                     ^  \ rS rSrSr SS\S\S\S\S\	SS	4U 4S
 jjjr
S\\\4   S\\\4   4S jrS\\\4   S\\\4   4S jrS\\\4   S	-  S\\\4   S	-  4S jr\S	S.S\S\S	-  S\\\4   S	-  4S jj5       r\S	S.S\\   S\S	-  S\\\\4   S	-     4S jj5       r\S	S.S\S\S	-  S\\\\4   S	-  \S	-  4   4S jj5       r\S	S.S\\   S\S	-  S\\\\\4   S	-  \S	-  4      4S jj5       r\S	S	S.S\S\\\4   S\S	-  S\S	-  SS	4
S jj5       r\S	S	S.S\\   S\\\\4      S\S	-  S\\S	-     S	-  SS	4
S jj5       rSrU =r$ )BaseEncryptionWrapper   a3  Wrapper that encrypts values before storing and decrypts on retrieval.

This wrapper encrypts the JSON-serialized value using a custom encryption function
and stores it as a base64-encoded string within a special key in the dictionary.
This allows encryption while maintaining the dict[str, Any] interface.
	key_valueencryption_fndecryption_fnencryption_versionraise_on_decryption_errorreturnNc                 ^   > Xl         XPl        X@l        X l        X0l        [
        TU ]  5         g)a  Initialize the encryption wrapper.

Args:
    key_value: The store to wrap.
    encryption_fn: The encryption function to use. A callable that takes bytes and returns encrypted bytes.
    decryption_fn: The decryption function to use. A callable that takes bytes and an
                   encryption version int and returns decrypted bytes.
    encryption_version: The encryption version to use.
    raise_on_decryption_error: Whether to raise an exception if decryption fails. Defaults to True.
N)r   r   r   _encryption_fn_decryption_fnsuper__init__)selfr   r   r   r   r   	__class__s         `/home/james-whalen/.local/lib/python3.13/site-packages/key_value/aio/wrappers/encryption/base.pyr   BaseEncryptionWrapper.__init__   s-    $ )2/H&'9,9,9    valuec                     [         R                  " USS9nUR                  SS9n U R                  U5      n[        R                  " U5      R                  SS9n[        U[        U R                  0$ ! [         R                  [        4 a  nSU 3n[        U5      UeSnAff = f! [         a  nSn[        U5      UeSnAff = f)	zEncrypt a value into the encrypted format.

The encrypted format looks like:
{
    "__encrypted_data__": "base64-encoded-encrypted-data",
    "__encryption_version__": 1
}
),:)
separatorsutf-8encodingz$Failed to serialize object to JSON: NasciizFailed to encrypt value)jsondumpsencodeJSONDecodeError	TypeErrorr   r   base64	b64encodedecode	Exceptionr
   _ENCRYPTED_DATA_KEY_ENCRYPTION_VERSION_KEYr   )r   r#   json_str
json_bytesemsgencrypted_bytes
base64_strs           r    _encrypt_value$BaseEncryptionWrapper._encrypt_value9   s    	1 JJuDH ( AJ
	.%)%8%8%DO$..?FFPWFXJ  #T%<%<
 	
 $$i0 	1=aSAC$S)q0	1  	.+C!#&A-	.s.   $A3 4B& 3B#BB#&
C0B>>Cc                 @   [         U;  a  Sn[        U5      eU[            n[        U[        5      (       d  S[	        U5       3n[        U5      e[
        U;  a  Sn[        U5      eU[
           n[        U[        5      (       d  S[	        U5       3n[        U5      eX44$ )Nzmissing encryption version keyz.expected encryption version to be an int, got zmissing encrypted data keyz)expected encrypted data to be a str, got )r6   r   
isinstanceinttyper5   str)r   r#   r:   r   encrypted_datas        r    _validate_encrypted_payload1BaseEncryptionWrapper._validate_encrypted_payloadY   s    "%/2C$S))"#:;,c22B4HZC[B\]C$S))e+.C$S))23.#..=d>>R=STC$S))!11r"   c                    Uc  g[         U;  a  [        U[        5      (       a  U$  U R                  U5      u  p#[        R
                  " USS9nU R                  XB5      nUR                  SS9n[        R                  " U5      $ ! [         a    U R                  (       a  e  g[         a)  nSnU R                  (       a  [        U5      Ue SnAgSnAff = f)z*Decrypt a value from the encrypted format.NT)validater(   r)   zFailed to decrypt value)r5   r@   dictrE   r1   	b64decoder   r3   r,   loadsr   r   r4   r	   )	r   r#   r   rD   r;   r8   r7   r9   r:   s	            r    _decrypt_value$BaseEncryptionWrapper._decrypt_valueo   s    = e+
5$0G0GL	151Q1QRW1X.%+%5%5nt%TO $ 3 3O XJ&--w-?H::h''! 	-- 	+C--%c*1		s   AB C#	C,CC)
collectionkeyrN   c                r   #    U R                   R                  XS9I S h  vN nU R                  U5      $  N7fN)rO   rN   )r   getrL   )r   rO   rN   r#   s       r    rR   BaseEncryptionWrapper.get   s6     nn((S(HH""5)) Is   757keysc                   #    U R                   R                  XS9I S h  vN nU Vs/ s H  o@R                  U5      PM     sn$  N%s  snf 7fN)rT   rN   )r   get_manyrL   )r   rT   rN   valuesr#   s        r    rW   BaseEncryptionWrapper.get_many   sF     ~~..D.PP8>?u##E*?? Q?s    AAAAAAc                z   #    U R                   R                  XS9I S h  vN u  p4U R                  U5      U4$  N7frQ   )r   ttlrL   )r   rO   rN   r#   r[   s        r    r[   BaseEncryptionWrapper.ttl   s=     >>--#-MM
""5)3.. Ns   ;9;c                   #    U R                   R                  XS9I S h  vN nU VVs/ s H  u  pEU R                  U5      U4PM     snn$  N,s  snnf 7frV   )r   ttl_manyrL   )r   rT   rN   resultsr#   r[   s         r    r^   BaseEncryptionWrapper.ttl_many   sO     //T/QQDKLGje$$U+S1GLL RLs    AA	AAAA)rN   r[   r[   c                t   #    U R                  U5      nU R                  R                  XX4S9I S h  vN $  N7f)N)rO   r#   rN   r[   )r=   r   put)r   rO   r#   rN   r[   encrypted_values         r    rb   BaseEncryptionWrapper.put   s6     --e4^^''CS]'ggggs   /868rX   c                   #    U Vs/ s H  oPR                  U5      PM     nnU R                  R                  XX4S9I S h  vN $ s  snf  N	7f)N)rT   rX   rN   r[   )r=   r   put_many)r   rT   rX   rN   r[   r#   encrypted_valuess          r    rf   BaseEncryptionWrapper.put_many   sN      EKKF5//6FK^^,,$\f,ppp Lps   AAA A

A)r   r   r   r   r   )T)__name__
__module____qualname____firstlineno____doc__r   EncryptionFnDecryptionFnrA   boolr   rI   rC   r   r=   tuplerE   rL   r   rR   listrW   floatr[   r^   r   rb   r   rf   __static_attributes____classcell__)r   s   @r    r   r      s    +/  $ $	
   $( 
 8
DcN 
tCH~ 
@2c3h 2E#s(O 2,DcNT$9 d38nt>S : >B *S *t *tCQTH~X\G\ * * JN @49 @S4Z @SWX\]`be]eXfimXmSn @ @ >B /S /t /uTRUWZRZ^^bMbdilpdpMpGq / / JN M49 MS4Z MSWX]^bcfhkck^los^suz  ~B  vB  _B  YC  TD M M UYw{ hS hc3h hd
 h`mpt`t h  AE h h  "&59	q3i	q c3h(	q
 $J	q md*+d2	q 
	q 	qr"   r   )r1   r,   collections.abcr   r   typingr   r   !key_value.shared.errors.key_valuer   +key_value.shared.errors.wrappers.encryptionr   r	   r
   typing_extensionsr   !key_value.aio.protocols.key_valuer   key_value.aio.wrappers.baser   r5   r6   bytesrn   rA   ro   r    r"   r    <module>r      sd      . % @ l l & ; 3* 2  'e+,ZqK Zqr"   