from typing import Union
import base64


def b64_encode(
    data: Union[bytes, str], encoding: str = "utf-8", padded: bool = True
) -> str:
    """
    Encodes the string or bytes `data` using Base64.
    If `data` is a string, encode it to bytes using `encoding` before converting it to Base64.
    If `padded` is `True` (default), outputs includes a padding with `=` to make its length a multiple of 4. If `False`,
    no padding is included.
    :param data: value to base64-encode.
    :param encoding: if `data` is a `str`, use this encoding to convert it to `bytes` first.
    :param padded: whether to include padding in the output
    :return: a `str` with the base64-encoded data.
    """
    if not isinstance(data, bytes):
        if not isinstance(data, str):
            data = str(data)
        data = data.encode(encoding)

    encoded = base64.b64encode(data)
    if not padded:
        encoded = encoded.rstrip(b"=")
    return encoded.decode("ascii")