o
    i"                     @  s   d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlZzd dlZeeds2dZW n ey>   dZY nw d"ddZd"ddZd#ddZd"ddZd"ddZd$ddZd%d&d d!ZdS )'    )annotationsN)HASHUnsupportedDigestmodError)Callable)Anyxxh3_128_digestinputr   returnbytesc                 C  s   t j| t jd}t| S )u  Hash any picklable Python object using SHA-256.

    The input is serialized using pickle before hashing, which allows
    arbitrary Python objects to be used. Note that this function does
    not use a hash seed—if you need one, prepend it explicitly to the input.

    Args:
        input: Any picklable Python object.

    Returns:
        Bytes representing the SHA-256 hash of the serialized input.
    protocol)pickledumpsHIGHEST_PROTOCOLhashlibsha256digestr   input_bytes r   H/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/utils/hashing.pyr      s   r   c                 C  s   t j| dd}t| S )a  Hash objects using CBOR serialization and SHA-256.

    This option is useful for non-Python-dependent serialization and hashing.

    Args:
        input: Object to be serialized and hashed. Supported types include
            basic Python types and complex structures like lists, tuples, and
            dictionaries.
            Custom classes must implement CBOR serialization methods.

    Returns:
        Bytes representing the SHA-256 hash of the CBOR serialized input.
    T	canonical)cbor2r   r   r   r   r   r   r   r   sha256_cbor+   s   r   r   c                 C  s   t d u rtdt | S )Nzhxxhash is required for the 'xxhash' prefix caching hash algorithms. Install it via `pip install xxhash`.)_xxhashModuleNotFoundErrorr   )r   r   r   r   _xxhash_digest=   s
   
r   c                 C  s   t j| t jd}t|S )z$Hash picklable objects using xxHash.r   )r   r   r   r   r   r   r   r   xxhashF   s   r   c                 C  s   t j| dd}t|S )z/Hash objects serialized with CBOR using xxHash.Tr   )r   r   r   r   r   r   r   xxhash_cborL   s   r   hash_fn_namestrCallable[[Any], bytes]c                 C  s>   | dkrt S | dkrtS | dkrtS | dkrtS td|  )zGet a hash function by name, or raise an error if the function is not found.

    Args:
        hash_fn_name: Name of the hash function.

    Returns:
        A hash function.
    r   r   r   r   zUnsupported hash function: )r   r   r   r   
ValueError)r    r   r   r   get_hash_fn_by_nameR   s   	r$   Tdatausedforsecurityboolr   c              	   C  s2   zt j| |dW S  ttfy   t |  Y S w )zHash for configs, defaulting to md5 but falling back to sha256
    in FIPS constrained environments.

    Args:
        data: bytes
        usedforsecurity: Whether the hash is used for security purposes

    Returns:
        Hash object
    )r&   )r   md5r   r#   r   )r%   r&   r   r   r   	safe_hashg   s
   r)   )r   r   r	   r
   )r   r
   r	   r
   )r    r!   r	   r"   )T)r%   r
   r&   r'   r	   r   )
__future__r   r   r   _hashlibr   r   collections.abcr   typingr   r   r   r   hasattrImportErrorr   r   r   r   r$   r)   r   r   r   r   <module>   s,   




	

