bases.encoding.zeropad

Zero-padded base encodings.

Similar to simple base encodings, but additionally:

  • preserves leading zeros

  • optionally enforces a fixed block size for encoded strings and decoded bytestrings

Constructor options:

  • block_nbytes: int number of bytes in a block for decoded bytestrings (default: 1)

  • block_nchars: int number of chars in a block for encoded strings (default: 1)

The static method ZeropadBaseEncoding.max_block_nchars (resp. ZeropadBaseEncoding.max_block_nbytes) gives the maximum block size in chars (resp. bytes) that can be used for a given block size in bytes (resp. chars). This is to ensure that encoding/decoding can always be performed unambiguously.

Encoding of a bytestring b:

  1. count the number Z of leading zero byte blocks in b and strip them (default: count zero bytes and strip them)

  2. encode b as a simple base encoding would

  3. prepend the minimum number of zero chars necessary to make the encoded string length an integral multiple of block_nchars

  4. prepend Z zero char blocks to the encoded string

Decoding of a string s:

  1. count the number Z of leading zero char blocks in s and strip them (default: count zero chars and strip them)

  2. decode s as a simple base encoding would

  3. prepend the minimum number of zero bytes necessary to make the decoded bytestring length an integral multiple of block_nbytes

  4. prepend Z zero byte blocks to the encoded string

ZeropadBaseEncoding

class ZeropadBaseEncoding(alphabet, *, case_sensitive=None, block_nbytes=1, block_nchars=1)[source]

Bases: BaseEncoding

Zero-added 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)

  • block_nbytes (int, optional) – number of bytes in a block for decoded bytestrings (default: 1)

  • block_nchars (int, optional) – number of chars in a block for encoded strings (default: 1)

property block_nbytes

Number of bytes in a block.

Return type:

int

property block_nchars

Number of characters in a block.

Return type:

int

static max_block_nbytes(base, block_nchars)[source]

Returns the maximum integer value for block_nbytes such that:

base**block_nchars > 256**(block_nbytes-1)
Parameters:
  • block_nchars (int) – number of chars in a block for encoded strings

  • base (int) –

Return type:

int

static max_block_nchars(base, block_nbytes)[source]

Returns the maximum integer value for block_chars such that:

256**block_nbytes > base**(block_nchars-1)
Parameters:
  • block_nbytes (int) – number of bytes in a block for decoded bytestrings

  • base (int) –

Return type:

int

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]