bases.encoding

Module containing classes for encodings.

BaseEncodingKind

BaseEncodingKind

Possible values for the kind keyword argument to make

alias of Literal[‘simple-enc’, ‘zeropad-enc’, ‘block-enc’, ‘fixchar-enc’]

base10

base10 = ZeropadBaseEncoding(StringAlphabet('0123456789'))

Base-10 encoding.

base16

base16 = ZeropadBaseEncoding(StringAlphabet('0123456789ABCDEF', case_sensitive=False), block_nchars=2)

Uppercase case-insensitive base-16 encoding (2-char blocks). This is the same encoding specified by rfc4648.

base2

base2 = ZeropadBaseEncoding(StringAlphabet('01'), block_nchars=8)

Base-2 encoding (8-char blocks).

base32

base32 = FixcharBaseEncoding(StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='include')

Uppercase case-insensitive base-32 encoding from rfc4648.

base32hex

base32hex = FixcharBaseEncoding(StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV', case_sensitive=False), pad_char='=', padding='include')

Uppercase case-insensitive hex base-32 encoding from rfc4648.

base32z

base32z = FixcharBaseEncoding(StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False))

Lowercase case-insensitive human-oriented base-32 encoding from https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt

base36

base36 = ZeropadBaseEncoding(StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', case_sensitive=False))

Uppercase case-insensitive base-36 alphabet.

base45

base45 = BlockBaseEncoding(StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:', case_sensitive=False), block_size={1: 2, 2: 3}, reverse_blocks=True)

Uppercase case-insensitive base-45 encoding from https://datatracker.ietf.org/doc/draft-faltstrom-base45/

Note that this is (slightly) different from the ISO/IEC 18004:2015 standard.

base58btc

base58btc = ZeropadBaseEncoding(StringAlphabet('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'))

Case-sensitive base-58 encoding used by Bitcoin.

Specs from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02

base58flickr

base58flickr = ZeropadBaseEncoding(StringAlphabet('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'))

Case-sensitive base-58 encoding used by Flickr.

Spec from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02 but using alphabet base58flickr.

base58ripple

base58ripple = ZeropadBaseEncoding(StringAlphabet('rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'))

Case-sensitive base-58 encoding used by Ripple.

Spec from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02 but using alphabet base58ripple.

base64

base64 = FixcharBaseEncoding(StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'), pad_char='=', padding='include')

Uppercase case-sensitive base-64 encoding from rfc4648.

base64url

base64url = FixcharBaseEncoding(StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'), pad_char='=', padding='include')

Uppercase case-sensitive url-safe base-64 encoding from rfc4648.

base8

base8 = FixcharBaseEncoding(StringAlphabet('01234567'), pad_char='=', padding='include')

Base-8 encoding from https://github.com/multiformats/multibase/blob/master/rfcs/Base8.md

get

get(name)[source]

Gets an encoding by name.

Example usage:

>>> bases.get("base16")
ZeropadBaseEncoding(
    StringAlphabet('0123456789ABCDEF',
                   case_sensitive=False),
    block_nchars=2)
Parameters:

name (str) – the encoding name

Raises:

KeyError – if an encoding by the given name does not exist

Return type:

BaseEncoding

has

has(name)[source]

Checks whether an encoding with the given name exists.

>>> bases.has("base32")
True
Parameters:

name (str) – the encoding name

Return type:

bool

make

make(chars, *, kind, name=None, case_sensitive=None, **kwargs)[source]

Utility function to create custom encodings.

The kind keyword argument can be used to select different encoding constructors:

If the optional keyword argument name is specified, the encoding is automatically registered using register.

The positional argument chars and the keyword argument case_sensitive are common to all encodings (with BlockBaseEncoding additionally accepting a BaseEncoding instance). The additional kwargs are those of each individual class constructor.

Example usage:

>>> from bases import encoding
>>> encoding.make("0123", kind="zeropad-enc", block_nchars=4, name="base4")
ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4)
>>> encoding.get("base4")
ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4)
>>> encoding.get("base4").encode(b"0x7E")
'0300132003131011'
Parameters:
  • chars (str, range, Alphabet or BaseEncoding) – the alphabet to use for the encoding, or a “block encoding” when 'kind' is set to 'block-enc'

  • kind ("simple-enc", "zeropad-enc", "block-enc" or "fixchar-enc") – whether the alphabet is case-sensitive

  • name (str or None, optional) – if specified, the newly created alphabet is registered with this name

  • case_sensitive (bool, optional) – whether the encoding alphabet is case-sensitive

  • kwargs (Dict[str, Any]) – additional keyword arguments to be passed to the chosen encoding constructor

Return type:

BaseEncoding

register

register(**encs)[source]

Registers any number of new encodings by name.

Example usage:

>>> bases.register(base16lower=encoding.base16.lower())
>>> bases.get("base16lower")
ZeropadBaseEncoding(
    StringAlphabet('0123456789abcdef',
                   case_sensitive=False),
    block_nchars=2)

Encoding names must conform with:

re.match(r"^base[0-9][a-zA-Z0-9_]*$", name)
Parameters:

encs (Dict[str, BaseEncoding]) – the encodings to register, passed by desired registration name

Raises:

KeyError – if an encoding with one of the given names already exists

Return type:

None

table

table(*, prefix='')[source]

Iterates over all registered encodings, optionally restricting to those with given prefix:

Example usage:

>>> from bases import encoding
>>> dict(encoding.table(prefix="base32"))
{'base32':      FixcharBaseEncoding(
                    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                                   case_sensitive=False),
                    pad_char='=', padding='include'),
 'base32hex':   FixcharBaseEncoding(
                    StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV',
                                   case_sensitive=False),
                    pad_char='=', padding='include'),
 'base32z':     FixcharBaseEncoding(
                    StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769',
                                   case_sensitive=False))
}
Parameters:
  • name (str, optional) – optional prefix to filter by when listing encodings

  • prefix (str; default = '') –

Return type:

Iterator[Tuple[str, BaseEncoding]]

unregister

unregister(*names)[source]

Unregisters any number of existing encodings by name.

Example usage:

>>> from bases import encoding
>>> encoding.unregister("base16", "base32")
>>> encoding.has("base16")
False
>>> encoding.get("base16")
KeyError: "Encoding named 'base16' does not exist."

Note that pre-defined constants are unaffected by this:

>>> encoding.base16
ZeropadBaseEncoding(
    StringAlphabet('0123456789ABCDEF',
                   case_sensitive=False),
    block_nchars=2)
Parameters:

names (Tuple[str, …]) – the encoding names

Raises:

KeyError – if an encoding with one of the given names does not exists

Return type:

None