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: intnumber of bytes in a block for decoded bytestrings (default: 1)block_nchars: intnumber 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:
count the number Z of leading zero byte blocks in
band strip them (default: count zero bytes and strip them)encode
bas asimplebase encoding wouldprepend the minimum number of zero chars necessary to make the encoded string length an integral multiple of
block_ncharsprepend Z zero char blocks to the encoded string
Decoding of a string s:
count the number Z of leading zero char blocks in
sand strip them (default: count zero chars and strip them)decode
sas asimplebase encoding wouldprepend the minimum number of zero bytes necessary to make the decoded bytestring length an integral multiple of
block_nbytesprepend Z zero byte blocks to the encoded string
ZeropadBaseEncoding
- class ZeropadBaseEncoding(alphabet, *, case_sensitive=None, block_nbytes=1, block_nchars=1)[source]
Bases:
BaseEncodingZero-added base encodings.
- Parameters:
alphabet (
str,rangeorAlphabet) – the alphabet to use for the encodingcase_sensitive (
boolorNone, optional) – optional case sensitivity (ifNone, 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)
- static max_block_nbytes(base, block_nchars)[source]
Returns the maximum integer value for
block_nbytessuch that:base**block_nchars > 256**(block_nbytes-1)
- static max_block_nchars(base, block_nbytes)[source]
Returns the maximum integer value for
block_charssuch that:256**block_nbytes > base**(block_nchars-1)