from __future__ import annotations

import hashlib
from typing import Any, Union

from .canonicalize import canonicalize
from .encoding import encode_base64url, utf8_encode
from .types import HashObject


def sha256(data: bytes) -> bytes:
    return hashlib.sha256(data).digest()


def hash_input(data: Any) -> HashObject:
    bytes_data = utf8_encode(canonicalize(data))
    return HashObject(alg="sha256", digest=encode_base64url(sha256(bytes_data)))


def hash_output(data: Union[str, bytes, bytearray]) -> HashObject:
    bytes_data = utf8_encode(data) if isinstance(data, str) else bytes(data)
    return HashObject(alg="sha256", digest=encode_base64url(sha256(bytes_data)))


hashInput = hash_input
hashOutput = hash_output

__all__ = ["sha256", "hash_input", "hash_output", "hashInput", "hashOutput"]
