def encode_randomness(randomness: hints.Buffer) -> str:
    """
    Encode the given buffer to a :class:`~str` using Base32 encoding.

    The given :class:`~bytes` are expected to represent the last 10 bytes of a ULID, which
    are cryptographically secure random values.

    .. note:: This uses an optimized strategy from the `NUlid` project for encoding ULID
        bytes specifically and is not meant for arbitrary encoding.

    :param randomness: Bytes to encode
    :type randomness: :class:`~bytes`, :class:`~bytearray`, or :class:`~memoryview`
    :return: Value encoded as a Base32 string
    :rtype: :class:`~str`
    :raises ValueError: when the randomness is not 10 bytes
    """
    length = len(randomness)
    if length != 10:
        raise ValueError('Expects 10 bytes for randomness; got {}'.format(length))

    encoding = ENCODING

    return \
        encoding[(randomness[0] & 248) >> 3] + \
        encoding[((randomness[0] & 7) << 2) | ((randomness[1] & 192) >> 6)] + \
        encoding[(randomness[1] & 62) >> 1] + \
        encoding[((randomness[1] & 1) << 4) | ((randomness[2] & 240) >> 4)] + \
        encoding[((randomness[2] & 15) << 1) | ((randomness[3] & 128) >> 7)] + \
        encoding[(randomness[3] & 124) >> 2] + \
        encoding[((randomness[3] & 3) << 3) | ((randomness[4] & 224) >> 5)] + \
        encoding[randomness[4] & 31] + \
        encoding[(randomness[5] & 248) >> 3] + \
        encoding[((randomness[5] & 7) << 2) | ((randomness[6] & 192) >> 6)] + \
        encoding[(randomness[6] & 62) >> 1] + \
        encoding[((randomness[6] & 1) << 4) | ((randomness[7] & 240) >> 4)] + \
        encoding[((randomness[7] & 15) << 1) | ((randomness[8] & 128) >> 7)] + \
        encoding[(randomness[8] & 124) >> 2] + \
        encoding[((randomness[8] & 3) << 3) | ((randomness[9] & 224) >> 5)] + \
        encoding[randomness[9] & 31]
def encode_randomness ( randomness : hints . Buffer ) -> str : length = len ( randomness ) if length != 10 : raise ValueError ( 'Expects 10 bytes for randomness; got {}' . format ( length ) ) encoding = ENCODING return encoding [ ( randomness [ 0 ] & 248 ) >> 3 ] + encoding [ ( ( randomness [ 0 ] & 7 ) << 2 ) | ( ( randomness [ 1 ] & 192 ) >> 6 ) ] + encoding [ ( randomness [ 1 ] & 62 ) >> 1 ] + encoding [ ( ( randomness [ 1 ] & 1 ) << 4 ) | ( ( randomness [ 2 ] & 240 ) >> 4 ) ] + encoding [ ( ( randomness [ 2 ] & 15 ) << 1 ) | ( ( randomness [ 3 ] & 128 ) >> 7 ) ] + encoding [ ( randomness [ 3 ] & 124 ) >> 2 ] + encoding [ ( ( randomness [ 3 ] & 3 ) << 3 ) | ( ( randomness [ 4 ] & 224 ) >> 5 ) ] + encoding [ randomness [ 4 ] & 31 ] + encoding [ ( randomness [ 5 ] & 248 ) >> 3 ] + encoding [ ( ( randomness [ 5 ] & 7 ) << 2 ) | ( ( randomness [ 6 ] & 192 ) >> 6 ) ] + encoding [ ( randomness [ 6 ] & 62 ) >> 1 ] + encoding [ ( ( randomness [ 6 ] & 1 ) << 4 ) | ( ( randomness [ 7 ] & 240 ) >> 4 ) ] + encoding [ ( ( randomness [ 7 ] & 15 ) << 1 ) | ( ( randomness [ 8 ] & 128 ) >> 7 ) ] + encoding [ ( randomness [ 8 ] & 124 ) >> 2 ] + encoding [ ( ( randomness [ 8 ] & 3 ) << 3 ) | ( ( randomness [ 9 ] & 224 ) >> 5 ) ] + encoding [ randomness [ 9 ] & 31 ]
Encode the given buffer to a :class:`~str` using Base32 encoding.

    The given :class:`~bytes` are expected to represent the last 10 bytes of a ULID, which
    are cryptographically secure random values.

    .. note:: This uses an optimized strategy from the `NUlid` project for encoding ULID
        bytes specifically and is not meant for arbitrary encoding.

    :param randomness: Bytes to encode
    :type randomness: :class:`~bytes`, :class:`~bytearray`, or :class:`~memoryview`
    :return: Value encoded as a Base32 string
    :rtype: :class:`~str`
    :raises ValueError: when the randomness is not 10 bytes