bases.encoding.simple

Simple base encodings.

Encoding of a bytestring b:

  1. if b contains any leading zero bytes, raises EncodingError

  2. converts b to an unsigned integer i (big-endian)

  3. converts i to the encoding base, using the encoding alphabet for digits

Decoding of a string s:

  1. if s contains any leading zero characters, raises DecodingError

  2. converts s to an unsigned integer i, using the encoding alphabet for digits of the encoding base

  3. converts i to its minimal byte representation (big-endian)

SimpleBaseEncoding

class SimpleBaseEncoding(alphabet, *, case_sensitive=None)[source]

Bases: BaseEncoding

Simple base encodings.

Parameters:
  • alphabet (str, range or Alphabet) – the alphabet to use for the encoding

  • case_sensitive (bool or None, optional) – optional case sensitivity (if None, the one from the alphabet is used)

canonical_bytes(b)[source]

Returns a canonical version of the bytestring b: this is the bytestring obtained by first encoding b and then decoding it.

(This method is overridden by subclasses with more efficient implementations.)

Parameters:

b (BytesLike) – the bytestring

Return type:

bytes

canonical_string(s)[source]

Returns a canonical version of the string s: this is the string obtained by first decoding s and then encoding it.

(This method is overridden by subclasses with more efficient implementations.)

Parameters:

s (str) – the string

Return type:

str

options(skip_defaults=False)[source]

The options used to construct this particular encoding.

Example usage:

>>> encoding.base32.options()
{'char_nbits': 'auto', 'pad_char': '=', 'padding': 'include'}
>>> encoding.base32.options(skip_defaults=True)
{'pad_char': '=', 'padding': 'include'}
Parameters:

skip_defaults (bool, optional) – if set to True, only options with non-default values are included in the mapping

Return type:

Mapping[str, Any]