tet.util.base64 module

Base64 and Crockford Base32 encoding utilities.

This module provides encoding utilities including standard Base64 and Crockford’s Base32, which is human-friendly (avoids ambiguous characters like 0/O and 1/I/L).

Example

Using standard Base64:

from tet.util.base64 import Base64

encoded = Base64.encode(b"hello")
decoded = Base64.decode(encoded)

# Generate random Base64 characters
random_str = Base64.generate_characters(16)

Using Crockford Base32:

from tet.util.base64 import CrockfordBase32

encoded = CrockfordBase32.encode(b"hello")
decoded = CrockfordBase32.decode(encoded)

# Crockford Base32 is case-insensitive and handles ambiguous chars
CrockfordBase32.decode("O1L")  # Treated as "011"
class tet.util.base64.Base64[source]

Bases: BaseCodec

Standard Base64 encoding.

bits_per_char = 6

Number of bits each encoded character carries (log2 of the alphabet size). Subclasses with power-of-two alphabets set this explicitly.

chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/'
classmethod decode(string)[source]

Decode Base64 to bytes.

classmethod encode(string)[source]

Encode bytes to Base64.

classmethod normalize(string)[source]

Normalize input (no-op for standard Base64).

padding = True
class tet.util.base64.BaseCodec[source]

Bases: object

Base class for encoding codecs.

bits_per_char = None

Number of bits each encoded character carries (log2 of the alphabet size). Subclasses with power-of-two alphabets set this explicitly.

classmethod generate_characters(length)[source]

Generate a random string of length characters in this codec’s alphabet, drawn from a cryptographically secure source.

Random bytes are generated with secrets.token_bytes(), run through the codec’s own encode(), and truncated to length. Each encoded character carries cls.bits_per_char bits, so ceil(length * bits_per_char / 8) input bytes always encode to at least length data characters. Padding (if any) only trails the data, so the truncated slice never includes it.

class tet.util.base64.CrockfordBase32[source]

Bases: BaseCodec

Crockford’s Base32 encoding.

Human-friendly encoding that avoids ambiguous characters (0/O, 1/I/L). Case-insensitive and handles common transcription errors.

bits_per_char = 5

Number of bits each encoded character carries (log2 of the alphabet size). Subclasses with power-of-two alphabets set this explicitly.

chars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
classmethod decode(string, normalize=True, validate=False)[source]

Decode Crockford Base32 to bytes.

classmethod encode(string, normalize=True, validate=False)[source]

Encode bytes to Crockford Base32.

classmethod normalize(string)[source]

Normalize input by handling ambiguous characters (O->0, I/L->1).

padding = False
tet.util.base64.maketrans(frm, to, /)

Return a translation table useable for the bytes or bytearray translate method.

The returned table will be one where each byte in frm is mapped to the byte at the same position in to.

The bytes objects frm and to must be of the same length.