"""RFC8785 JSON canonicalization and digest computation."""

from __future__ import annotations

import hashlib
from typing import Any

import canonicaljson


def canonicalize(data: dict[str, Any]) -> bytes:
    """
    Canonicalize a dictionary using RFC8785 JSON Canonicalization Scheme.

    Args:
        data: Dictionary to canonicalize

    Returns:
        Canonical JSON bytes
    """
    return canonicaljson.encode_canonical_json(data)


def compute_digest(data: dict[str, Any]) -> str:
    """
    Compute SHA-256 digest of RFC8785-canonicalized data.

    Args:
        data: Dictionary to canonicalize and hash

    Returns:
        Lowercase hex digest (64 characters)
    """
    canonical = canonicalize(data)
    return hashlib.sha256(canonical).hexdigest()


def compute_digest_from_bytes(data: bytes) -> str:
    """
    Compute SHA-256 digest of bytes.

    Args:
        data: Bytes to hash

    Returns:
        Lowercase hex digest (64 characters)
    """
    return hashlib.sha256(data).hexdigest()
