o
    iC                     @   s   d dl Z d dlmZ dZdZdZdZdZdZd	Z	ee e	 Z
efd
ededefddZG dd deeZd
edededefddZdeeejefd
edededB dededededefddZd
ededefddZdS )    N)Enum      -_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789valuesizereturnc                 C   s   t |   d| S )z&Generate a hash fragment of the value.N)hashlibsha256encode	hexdigestr
   r    r   e/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/key_value/shared/utils/sanitize.pygenerate_hash_fragment   s   r   c                   @   s   e Zd ZdZdZdZdS )HashFragmentModealwaysneveronly_if_changedN)__name__
__module____qualname__ALWAYSNEVERONLY_IF_CHANGEDr   r   r   r   r      s    r   allowed_charactersreplace_withc                 C   sR   d}d}| D ]}||v r||7 }d}q|rq||7 }d}qt |dkr'|r'dS |S )a  Replace characters in a string. If multiple characters are in a row that are not allowed, only
    the first one will be replaced. The rest will be removed. If all characters are not allowed, an
    empty string will be returned.

    Args:
        value: The value to replace characters in.
        to_replace: The characters to replace.
        replace_with: The characters to replace with.
     FT   )len)r
   r   r    	new_valuelast_char_was_replacedcharr   r   r   sanitize_characters_in_string    s   
r'   
max_lengthreplacement_characterhash_fragment_separatorhash_fragment_modehash_fragment_lengthc                 C   s   |t k rdt  }t|||d krd}t|t| |d}t|t| }	|r0t| ||dn| }
|tjkrK||	 }|
d| }
|
sE|S |
| | S |tjkrp|
d| }
| |
kr\| S ||	 }|
d| }
|
sj|S |
| | S |
sxd}t||
S )a  Sanitize the value, replacing characters and optionally adding a fragment a hash of the value if requested.

    If the entire value is sanitized and hash_fragment_mode is HashFragmentMode.ALWAYS or HashFragmentMode.ONLY_IF_CHANGED,
    the value returned will be the hash fragment only.

    If the entire value is sanitized and hash_fragment_mode is HashFragmentMode.NEVER, an error will be raised.

    Args:
        value: The value to sanitize.
        allowed_characters: The allowed characters in the value.
        max_length: The maximum length of the value (with the hash fragment added).
        hash_fragment_separator: The separator to add between the value and the hash fragment.
        hash_fragment_mode: The mode to add the hash fragment.
    z,max_length must be greater than or equal to    zEhash_fragment_length must be less than or equal to half of max_lengthr   )r
   r   r    NzKEntire value was sanitized and hash_fragment_mode is HashFragmentMode.NEVER)MINIMUM_MAX_LENGTH
ValueErrorr   r#   r'   r   r   r   )r
   r(   r   r)   r*   r+   r,   msghash_fragmenthash_fragment_size_requiredsanitized_valueactual_max_lengthr   r   r   sanitize_string>   s>   


r5   c                 C   s`   |t krdt  }t|t| |kr| S | d|d  }t|   }|dd }|| S )a  Hash part of the value if it exceeds the maximum length. This operation
    will truncate the value to the maximum length minus 8 characters and will swap
    the last 8 characters with the first 8 characters of the generated hash.

    Args:
        value: The value to hash.
        max_length: The maximum length of the value. Must be greater than 32.

    Returns:
        The hashed value if the value exceeds the maximum length, otherwise the original value.
    z max_length must be greater than Nr   )r.   r/   r#   r   r   r   r   )r
   r(   r0   truncated_valuehash_of_valuefirst_eight_of_hashr   r   r   hash_excess_length   s   
r9   )r   enumr   r.   DEFAULT_HASH_FRAGMENT_SIZEDEFAULT_HASH_FRAGMENT_SEPARATORDEFAULT_REPLACEMENT_CHARACTERLOWERCASE_ALPHABETUPPERCASE_ALPHABETNUMBERSALPHANUMERIC_CHARACTERSstrintr   r   r'   r   r5   r9   r   r   r   r   <module>   sV    
	!
J