
    h)
                     d    d dl Z d dlZd dlmZ d dlmZ dedefdZdededefd	Z	d
ededefdZ
y)    N)DEFAULT_ALPHABET)Sqidssreturnc                     d}t        |       D ]  }|dz  t        |      z  }|dz  } |dz  |dz	  dz  z  }|dk\  r|dz  }|S )	a  
    Compute a hash of the input string using the djb2 algorithm.

    This implementation processes the characters in reverse order and
    applies the djb2 algorithm with a twist for 32-bit overflow handling.
    It then adjusts the result to a signed 32-bit integer.

    Args:
        s (str): The input string to be hashed.

    Returns:
        int: A signed 32-bit hash of the input string.
    i  !   l    l       i   @l        l        )reversedord)r   hchars      Q/var/www/Befach/backend/env/lib/python3.12/site-packages/upyloadthing/file_key.pydjb2r      sc     	AVs4y 	Z  
ZQ!Vz12A 	J	[H    stringseedc           
      (   t        |       }t        |      }t        t        |            D ]U  }t	        t        j                  t        j                  ||dz         |z   t        |                  }||   ||   c||<   ||<   W dj                  |      S )a  
    Shuffle the characters of the given string using a seed.

    The shuffling process is based on a numeric seed derived from the
    djb2 hash of the provided seed string, ensuring that the same seed
    produces the same shuffled result for a given string.

    Args:
        string (str): The string to be shuffled.
        seed (str): The seed string used to influence the shuffling.

    Returns:
        str: The shuffled string.
    r	    )listr   rangelenintmathfmodjoin)r   r   charsseed_numijs         r   shuffler    $   s     LEDzH3u:		$))Ha!e4q8#e*EF"1XuQxa%(  775>r   	file_seedapp_idc                     t        t        |      }t        |d      j                  t	        t        |            g      }t        j                  | j                               j                         }||z   S )a  
    Generate a unique key using a file seed and an application identifier.

    This function generates a unique key by first shuffling the default
    alphabet using the application id as a seed. It then encodes the absolute
    value of the djb2 hash of the application id using the Sqids algorithm and
    a minimum length of 12. Finally, it encodes the file seed with Base64
    in a URL-safe manner and concatenates the two encoded strings.

    Args:
        file_seed (str): A seed string to represent or identify the file.
        app_id (str): The application identifier used to influence the key
                      generation and alphabet shuffling.

    Returns:
        str: The generated unique key.
       )
min_length)	r    r   r   encodeabsr   base64urlsafe_b64encodedecode)r!   r"   alphabetencoded_app_idencoded_file_seeds        r   generate_keyr.   =   sc    $ '0H83::CV<M;NON001A1A1CDKKM---r   )r(   r   sqids.constantsr   sqids.sqidsr   strr   r   r    r.    r   r   <module>r3      sX      , C C 8C s s 2.C . . .r   