o
    i                     @   sr   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 dZdZG dd	 d	e
Zd
ededefddZdS )    )FernetMultiFernet)EncryptionVersionError)overload)AsyncKeyValue)BaseEncryptionWrapper   iO c                       s   e Zd ZdZedddedeeB deddfd	d
Z	edddede
de
deddf
dd
Z	ddddddedeeB dB de
dB de
dB deddf fdd
Z	  ZS )FernetEncryptionWrapperzjWrapper that encrypts values before storing and decrypts on retrieval using Fernet (symmetric encryption).T)raise_on_decryption_error	key_valuefernetr
   returnNc                C      dS )a  Initialize the Fernet encryption wrapper.

        Args:
            key_value: The key-value store to wrap.
            fernet: The Fernet or MultiFernet instance to use for encryption and decryption MultiFernet is used to support
                    key rotation by allowing you to provide multiple Fernet instances that are attempted in order.
            raise_on_decryption_error: Whether to raise an exception if decryption fails. Defaults to True.
        N )selfr   r   r
   r   r   n/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/key_value/aio/wrappers/encryption/fernet.py__init__       z FernetEncryptionWrapper.__init__source_materialsaltc                C   r   )a  Initialize the Fernet encryption wrapper.

        Args:
            key_value: The key-value store to wrap.
            source_material: A string to use as the source material for the encryption key.
            salt: A string to use as the salt for the encryption key.
            raise_on_decryption_error: Whether to raise an exception if decryption fails. Defaults to True.
        Nr   )r   r   r   r   r
   r   r   r   r   !   r   )r   r   r   r
   c          	         s    d ur|s|rd}t | d u r3|r| sd}t ||r$| s*d}t |tt||dd dtdtf fdd	}dtd
tdtf fdd}t j|||t|d d S )Nz;Cannot provide fernet together with source_material or saltz-Must provide either fernet or source_materialzMust provide a salt)r   r   )keydatar   c                    s
     | S )N)encrypt)r   )r   r   r   encrypt_with_fernetJ   s   
z=FernetEncryptionWrapper.__init__.<locals>.encrypt_with_fernetencryption_versionc                    s*   |j krdj  d}t| | S )Nz2Decryption failed: encryption versions newer than z are not supported)r   r   decrypt)r   r   msgr   r   r   r   decrypt_with_fernetM   s   

z=FernetEncryptionWrapper.__init__.<locals>.decrypt_with_fernet)r   encryption_fndecryption_fnr   r
   )	
ValueErrorstripr   _generate_encryption_keybytesintsuperr   ENCRYPTION_VERSION)	r   r   r   r   r   r
   r   r   r   	__class__r   r   r   3   s*   	
)__name__
__module____qualname____doc__r   r   r   r   boolr   str__classcell__r   r   r(   r   r	      sV    
r	   r   r   r   c                 C   sN   ddl }ddlm} ddlm} || d| tdj|  d}|	|S )zNGenerate a Fernet encryption key from a source material and salt using PBKDF2.r   N)hashes)
PBKDF2HMAC    )	algorithmlengthr   
iterations)key_material)
base64cryptography.hazmat.primitivesr1   )cryptography.hazmat.primitives.kdf.pbkdf2r2   SHA256encodeKDF_ITERATIONSderiveurlsafe_b64encode)r   r   r8   r1   r2   pbkdf2r   r   r   r#   \   s   
r#   N)cryptography.fernetr   r   +key_value.shared.errors.wrappers.encryptionr   typing_extensionsr   !key_value.aio.protocols.key_valuer   &key_value.aio.wrappers.encryption.baser   r'   r=   r	   r/   r$   r#   r   r   r   r   <module>   s    O