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
:
count the number Z of leading zero byte blocks in
b
and strip them (default: count zero bytes and strip them)encode
b
as asimple
base encoding wouldprepend the minimum number of zero chars necessary to make the encoded string length an integral multiple of
block_nchars
prepend Z zero char blocks to the encoded string
Decoding of a string s
:
count the number Z of leading zero char blocks in
s
and strip them (default: count zero chars and strip them)decode
s
as asimple
base encoding wouldprepend the minimum number of zero bytes necessary to make the decoded bytestring length an integral multiple of
block_nbytes
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
orAlphabet
) – the alphabet to use for the encodingcase_sensitive (
bool
orNone
, 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_nbytes
such that:base**block_nchars > 256**(block_nbytes-1)
- 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)