o
    "i                     @   sZ   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e	e
ZG dd dZe ZdS )z)
Encryption utilities for sensitive data
    )Fernet)settings)OptionalNc                   @   sV   e Zd ZdZdd Zdd Zdedefdd	Zd
edefddZdede	fddZ
dS )EncryptionServicez4Service for encrypting and decrypting sensitive datac                 C   s   d | _ |   d S )N)_fernet_initialize)self r	   ;/lsinfo/ai/hellotax_ai/base_platform/app/core/encryption.py__init__   s   zEncryptionService.__init__c              
   C   sn   t js
td dS zt j}t|tr| }t|| _W dS  t	y6 } zt
d|  tdd}~ww )zInitialize Fernet cipherzENCRYPTION_KEY not configured. API keys will be stored in plaintext. Generate a key with: python -c 'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'Nz!Failed to initialize encryption: zInvalid ENCRYPTION_KEY. Generate a new key with: python -c 'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())')r   ENCRYPTION_KEYloggerwarning
isinstancestrencoder   r   	Exceptionerror
ValueError)r   keyer	   r	   r
   r      s"   
zEncryptionService._initialize	plaintextreturnc              
   C   sb   |s|S | j std |S z| j | }| W S  ty0 } z	td|   d}~ww )z
        Encrypt a string

        Args:
            plaintext: The string to encrypt

        Returns:
            Base64-encoded encrypted string
        z,Encryption not configured, storing plaintextzEncryption failed: N)r   r   r   encryptr   decoder   r   )r   r   	encryptedr   r	   r	   r
   r   *   s   


zEncryptionService.encrypt
ciphertextc              
   C   sp   |s|S | j std |S z| j | }| W S  ty7 } ztd|  |W  Y d}~S d}~ww )z
        Decrypt a string

        Args:
            ciphertext: The encrypted string to decrypt

        Returns:
            Decrypted plaintext string
        z0Encryption not configured, returning value as-isz*Decryption failed, returning value as-is: N)r   r   r   decryptr   r   r   )r   r   	decryptedr   r	   r	   r
   r   B   s   


zEncryptionService.decryptvaluec                 C   s:   |r| j sdS z| j |  W dS  ty   Y dS w )z
        Check if a value appears to be encrypted

        Args:
            value: The value to check

        Returns:
            True if the value appears to be encrypted
        FT)r   r   r   r   )r   r   r	   r	   r
   is_encrypted[   s   

zEncryptionService.is_encryptedN)__name__
__module____qualname____doc__r   r   r   r   r   boolr    r	   r	   r	   r
   r      s    r   )r$   cryptography.fernetr   
app.configr   typingr   base64logging	getLoggerr!   r   r   encryption_servicer	   r	   r	   r
   <module>   s    

d