import base64
import hashlib


def onion_hostname(onion_public_key: bytes) -> str:
    p = onion_public_key[32:]

    h = hashlib.sha3_256()
    h.update(b".onion checksum")
    h.update(p)
    h.update(b"\x03")
    checksum = h.digest()

    result = bytearray(p)
    result.extend(checksum[0:2])
    result.append(0x03)

    onion = base64.b32encode(result).decode("utf-8").strip("=")
    return onion.lower()
