
    k7iC                         S SK r S SKJr  SrSrSrSrSrSrS	r	\\-   \	-   r
\4S
\S\S\4S jjr " S S\\5      rS
\S\S\S\4S jrS\\\R                   \4S
\S\S\S-  S\S\S\S\S\4S jjrS
\S\S\4S jrg)    N)Enum      -_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789valuesizereturnc                 l    [         R                  " U R                  5       5      R                  5       SU $ )z&Generate a hash fragment of the value.N)hashlibsha256encode	hexdigestr   r   s     Y/home/james-whalen/.local/lib/python3.13/site-packages/key_value/shared/utils/sanitize.pygenerate_hash_fragmentr      s)     >>%,,.)335et<<    c                        \ rS rSrSrSrSrSrg)HashFragmentMode   alwaysneveronly_if_changed N)__name__
__module____qualname____firstlineno__ALWAYSNEVERONLY_IF_CHANGED__static_attributes__r   r   r   r   r      s    FE'Or   r   allowed_charactersreplace_withc                     SnSnU  H  nXQ;   a  X5-  nSnM  U(       a  M  X2-  nSnM!     [        U5      S:X  a  U(       a  gU$ )aq  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chars         r   sanitize_characters_in_stringr/       s[     I"%I%*"%%I%)"  9~5r   
max_lengthreplacement_characterhash_fragment_separatorhash_fragment_modehash_fragment_lengthc                    U[         :  a  S[          3n[        U5      eXaS-  :  a  Sn[        U5      e[        XS9n[        U5      [        U5      -   n	U(       a
  [	        XUS9OU n
U[
        R                  :X  a  X-
  nU
SU n
U
(       d  U$ X-   U-   $ U[
        R                  :X  a%  U
SU n
X
:X  a  U $ X-
  nU
SU n
U
(       d  U$ X-   U-   $ U
(       d  Sn[        U5      eU
$ )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   r0   r&   r1   r2   r3   r4   msghash_fragmenthash_fragment_size_requiredsanitized_valueactual_max_lengths               r   sanitize_stringr>   >   s#   . &&<=O<PQoAo-Uo/eWM'*+B'Cc-FX'X  	&Eg|}  -444&D)*<+<=  8=HH-===)+:6#L&D)*<+<=  8=HH[or   c                     U[         ::  a  S[          3n[        U5      e[        U 5      U::  a  U $ U SUS-
   n[        R                  " U R                  5       5      R                  5       nUSS nX5-   $ )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   )r7   r8   r+   r   r   r   r   )r   r0   r9   truncated_valuehash_of_valuefirst_eight_of_hashs         r   hash_excess_lengthrC      sz     ''01C0DEo
5zZ,j1n-ONN5<<>2<<>M'+00r   )r   enumr   r7   DEFAULT_HASH_FRAGMENT_SIZEDEFAULT_HASH_FRAGMENT_SEPARATORDEFAULT_REPLACEMENT_CHARACTERLOWERCASE_ALPHABETUPPERCASE_ALPHABETNUMBERSALPHANUMERIC_CHARACTERSstrintr   r   r/   r$   r>   rC   r   r   r   <module>rN      s+      "%  # 1 1 
,/AAGK 
 +==
= 	=(sD ( # UX ]` B &*!>#B+;+K+K :GGG d
G 	G
 !G )G G 	GT1c 1s 1s 1r   