
    :j                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZ G d d      Z	 e	d      Z
dd	ed
edefdZdded
edefdZdded
edefdZy)z
Encryption utility module for BookBeach application
Provides encryption and decryption functionality using Fernet symmetric encryption
    )Fernet)hashes)
PBKDF2HMACNc                   X    e Zd ZdZdefdZdefdZdedefdZdedefd	Z	d
ede
fdZy)PasswordEncryptionz5Handle password encryption and decryption using a key
key_phrasec                 <    || _         | j                         | _        y)z
        Initialize the encryption handler with a key phrase
        
        Args:
            key_phrase (str): The key phrase to use for encryption/decryption
        N)r   _create_fernet_key_fernet)selfr   s     6/var/www/bookbeach.app/backend/app/utils/encryption.py__init__zPasswordEncryption.__init__   s     %..0    returnc                     d}t        t        j                         d|d      }t        j                  |j                  | j                  j                                     }t        |      S )z'Create a Fernet key from the key phrases   bookbeach_salt_123456789012345    i )	algorithmlengthsalt
iterations)	r   r   SHA256base64urlsafe_b64encodederiver   encoder   )r   r   kdfkeys       r   r
   z%PasswordEncryption._create_fernet_key   sW     1mmo	
 &&szz$//2H2H2J'KLc{r   	plaintextc                     |sy| j                   j                  |j                               }t        j                  |      j                         S )z
        Encrypt a plaintext string
        
        Args:
            plaintext (str): The string to encrypt
            
        Returns:
            str: The encrypted string (base64 encoded)
         )r   encryptr   r   r   decode)r   r   encrypted_bytess      r   r!   zPasswordEncryption.encrypt(   sA     ,,..y/?/?/AB''8??AAr   encrypted_textc                     |sy	 t        j                  |j                               }| j                  j	                  |      }|j                         S # t        $ r}t        dt        |             d}~ww xY w)z
        Decrypt an encrypted string
        
        Args:
            encrypted_text (str): The encrypted string (base64 encoded)
            
        Returns:
            str: The decrypted plaintext string
        r    zFailed to decrypt text: N)	r   urlsafe_b64decoder   r   decryptr"   	Exception
ValueErrorstr)r   r$   r#   decrypted_byteses        r   r'   zPasswordEncryption.decrypt8   su     	B$66~7L7L7NOO"ll22?CO"))++ 	B7Ax@AA	Bs   AA 	A8A33A8textc                 <    |sy	 | j                  |       y#  Y yxY w)z
        Check if a string appears to be encrypted
        
        Args:
            text (str): The string to check
            
        Returns:
            bool: True if the string appears to be encrypted
        FT)r'   )r   r-   s     r   is_encryptedzPasswordEncryption.is_encryptedL   s(     	LL	s    N)__name__
__module____qualname____doc__r*   r   r   r
   r!   r'   boolr/    r   r   r   r      s\    ?13 1F B B B Bc Bc B(  r   r   	babagammapasswordr   r   c                 :    t        |      }|j                  |       S )z
    Encrypt a password using the specified key phrase
    
    Args:
        password (str): The password to encrypt
        key_phrase (str): The key phrase to use for encryption
        
    Returns:
        str: The encrypted password
    )r   r!   )r7   r   
encryptions      r   encrypt_passwordr:   e   s     $J/Jh''r   encrypted_passwordc                 :    t        |      }|j                  |       S )z
    Decrypt a password using the specified key phrase
    
    Args:
        encrypted_password (str): The encrypted password
        key_phrase (str): The key phrase to use for decryption
        
    Returns:
        str: The decrypted password
    )r   r'   )r;   r   r9   s      r   decrypt_passwordr=   t   s      $J/J011r   r-   c                 :    t        |      }|j                  |       S )z
    Check if a string is encrypted
    
    Args:
        text (str): The string to check
        key_phrase (str): The key phrase to use for testing decryption
        
    Returns:
        bool: True if the string appears to be encrypted
    )r   r/   )r-   r   r9   s      r   r/   r/      s     $J/J""4((r   )r6   )r3   cryptography.fernetr   cryptography.hazmat.primitivesr   )cryptography.hazmat.primitives.kdf.pbkdf2r   r   osr   DEFAULT_ENCRYPTIONr*   r:   r=   r4   r/   r5   r   r   <module>rD      s   
 ' 1 @  	Q Qj (4 (s ( (c (2 2# 2PS 2)s ) )d )r   