Skip to content

hash_service

Unified Hash Service for Cache Operations.

This module provides a centralized hash service that unifies all hash operations across the cache system using SHA256 as the standard algorithm.

Replaces: - UnifiedKeyGenerator.album_key() (MD5) → hash_album_key() (SHA256) - UnifiedKeyGenerator.hash_key() (SHA256) → hash_generic_key() (SHA256) - OptimizedHashGenerator.generate_key() (SHA256) → hash_api_key() (SHA256)

UnifiedHashService

Unified service for all cache hash operations using SHA256.

hash_album_key classmethod

hash_album_key(artist, album)

Generate SHA256 hash for album cache key.

Parameters:

Name Type Description Default
artist str

Artist name

required
album str

Album name

required

Returns:

Type Description
str

SHA256 hash string

Source code in src/services/cache/hash_service.py
@classmethod
def hash_album_key(cls, artist: str, album: str) -> str:
    """Generate SHA256 hash for album cache key.

    Args:
        artist: Artist name
        album: Album name

    Returns:
        SHA256 hash string
    """
    normalized_artist = normalize_for_matching(artist)
    normalized_album = normalize_for_matching(album)
    key_string = f"{normalized_artist}|{normalized_album}"

    return hashlib.sha256(key_string.encode()).hexdigest()

hash_api_key classmethod

hash_api_key(artist, album, source)

Generate SHA256 hash for API cache key.

Parameters:

Name Type Description Default
artist str

Artist name

required
album str

Album name

required
source str

API source identifier

required

Returns:

Type Description
str

SHA256 hash string

Source code in src/services/cache/hash_service.py
@classmethod
def hash_api_key(cls, artist: str, album: str, source: str) -> str:
    """Generate SHA256 hash for API cache key.

    Args:
        artist: Artist name
        album: Album name
        source: API source identifier

    Returns:
        SHA256 hash string
    """
    normalized_source = normalize_for_matching(source)
    normalized_artist = normalize_for_matching(artist)
    normalized_album = normalize_for_matching(album)
    key_string = f"{normalized_source}:{normalized_artist}|{normalized_album}"

    return hashlib.sha256(key_string.encode()).hexdigest()

hash_generic_key classmethod

hash_generic_key(data)

Generate SHA256 hash for generic cache key.

Parameters:

Name Type Description Default
data Any

Any data that can be converted to string

required

Returns:

Type Description
str

SHA256 hash string

Note

Non-JSON-serializable dict values are converted to their string representation.

Source code in src/services/cache/hash_service.py
@classmethod
def hash_generic_key(cls, data: Any) -> str:
    """Generate SHA256 hash for generic cache key.

    Args:
        data: Any data that can be converted to string

    Returns:
        SHA256 hash string

    Note:
        Non-JSON-serializable dict values are converted to their string representation.
    """
    # Handle different data types consistently (including nested dicts)
    # Using default=str ensures non-serializable values (datetime, Path, etc.) are converted
    key_string = json.dumps(data, sort_keys=True, default=str) if isinstance(data, dict) else str(data)
    return hashlib.sha256(key_string.encode()).hexdigest()

hash_pending_key classmethod

hash_pending_key(track_id)

Generate SHA256 hash for pending verification key.

Parameters:

Name Type Description Default
track_id str

Track identifier for pending verification

required

Returns:

Type Description
str

SHA256 hash string

Source code in src/services/cache/hash_service.py
@classmethod
def hash_pending_key(cls, track_id: str) -> str:
    """Generate SHA256 hash for pending verification key.

    Args:
        track_id: Track identifier for pending verification

    Returns:
        SHA256 hash string
    """
    key_string = f"pending:{track_id}"
    return hashlib.sha256(key_string.encode()).hexdigest()