o
    NDi                     @   s   d dl mZ d dlmZ d dlZd dlZeeZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZ dZeed	 Zd
Zeed	 ZedddgZG dd deZdS )    )
namedtuple)	lru_cacheN)	_cqltypes)ColumnEncryptionPolicy)padding)Cipher
algorithmsmodes         ColDatakeytypec                   @   sp   e Zd ZejZdddZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdddZedddd ZdS )AES256ColumnEncryptionPolicyNc                 C   s@   || _ | j rt| j tkstdntt| _ i | _i | _d S )Nz\This policy uses AES-256 with CBC mode and therefore expects a 128-bit initialization vector)ivlenAES256_BLOCK_SIZE_BYTES
ValueErrorosurandomcoldataciphers)selfr    r   ^/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/column_encryption/_policies.py__init__/   s   
z%AES256ColumnEncryptionPolicy.__init__c                 C   sJ   t t }|||  }| |}| }| j|| |  S N)	r   PKCS7AES256_BLOCK_SIZEpadderupdatefinalize_get_cipher	encryptorr   )r   coldesc	obj_bytesr    padded_bytescipherr$   r   r   r   encryptA   s
   	
z$AES256ColumnEncryptionPolicy.encryptc           	      C   s`   |d t  }|t d  }| j||d}| }|||  }tt }|||  S )N)r   )	r   r#   	decryptorr!   r"   r   r   r   unpadder)	r   r%   bytesr   encrypted_bytesr(   r*   r'   r+   r   r   r   decryptQ   s   z$AES256ColumnEncryptionPolicy.decryptc                 C   sj   |st d|st d|st d|t vrt d|t|tks)t dt|t| | j|< d S )Nz-ColDesc supplied to add_column cannot be Nonez)Key supplied to add_column cannot be Nonez*Type supplied to add_column cannot be NonezType %s is not a supported typez@AES256 column encryption policy expects a 256-bit encryption key)r   r   keysformatr   AES256_KEY_SIZE_BYTESr   r   )r   r%   r   r   r   r   r   
add_column\   s   z'AES256ColumnEncryptionPolicy.add_columnc                 C   s
   || j v S r   )r   r   r%   r   r   r   contains_columnj      
z,AES256ColumnEncryptionPolicy.contains_columnc                 C   sL   |st d|st d| j|}|st d|| ||j|d S )Nz5ColDesc supplied to encode_and_encrypt cannot be Nonez4Object supplied to encode_and_encrypt cannot be Nonez%Could not find ColData for ColDesc %s)r   r   getr0   r)   r   	serialize)r   r%   objr   r   r   r   encode_and_encryptm   s   z/AES256ColumnEncryptionPolicy.encode_and_encryptc                 C   s
   t j S r   )r   _build_cipher
cache_info)r   r   r   r   r;   w   r5   z'AES256ColumnEncryptionPolicy.cache_infoc                 C   s   | j | jS r   )r   r   r3   r   r   r   column_typez   s   z(AES256ColumnEncryptionPolicy.column_typec                 C   s>   z| j | }t|j|p| jW S  ty   td|w )z
        Access relevant state from this instance necessary to create a Cipher and then get one,
        hopefully returning a cached instance if we've already done so (and it hasn't been evicted)
        zCould not find column {})r   r   r:   r   r   KeyErrorr   r0   )r   r%   r   r   r   r   r   r#   }   s   
z(AES256ColumnEncryptionPolicy._get_cipherr
   )maxsizec                 C   s   t t| t|S r   )r   r   AES256r   mode)r   r   r   r   r   r:      s   z*AES256ColumnEncryptionPolicy._build_cipherr   )__name__
__module____qualname__r	   CBCr@   r   r)   r.   r2   r4   r9   r;   r<   r#   r   r:   r   r   r   r   r   $   s    


r   )collectionsr   	functoolsr   loggingr   	getLoggerrA   logcassandra.cqltypesr   cassandra.policiesr   cryptography.hazmat.primitivesr   &cryptography.hazmat.primitives.ciphersr   r   r	   r   intr   AES256_KEY_SIZEr1   r   r   r   r   r   r   <module>   s   
