bases.encoding.fixchar
Fixed-character base encodings, generalising those described by rfc4648.
Constructor options:
char_nbits: Union[int, Literal["auto"]], number of bits per character (default:"auto")pad_char: Optional[str], optional padding character (default:None)padding: PaddingOptions, padding style (default:"ignore")
If char_nbits is set to "auto" (by default), it is automatically computed as:
int(math.ceil(math.log2(base)))
From char_nbits, size of a block (in bytes and chars) is computed as:
block_nbytes = lcm(char_nbits, 8)//8
block_nchars = lcm(char_nbits, 8)//char_nbits
The value block_nbytes is presently not used, while the block_nchars is used for padding.
The padding option must be set to "ignore" if a padding character is not specified (i.e. if pad_char is None).
If a padding character is specified, it must be a character (string of length 1) not in the encoding alphabet:
it is allowed in decoding strings, but only at then end (so that s.rstrip(pad_char) removes all padding).
The padding behaviour is determined by the value of padding:
"ignore": no padding included on encoding, no padding required on decoding"include": padding included on encoding, no padding required on decoding"require": padding included on encoding, padding required on decoding
Encoding of a bytestring b:
compute the minimum number
extra_nbitsof additional bits necessary to make8*len(b)an integral multiple ofchar_nbitsconvert
bto an unsigned integeri(big-endian)left-shift
ibyextra_nbitsbits, introducing the necessary zero pad bitsconverts
ito the encoding base, using the encoding alphabet for digitsif
paddingis"include"or"require", append the minimum number of padding characters necessary to make the encoded string length an integral multiple ofblock_nchars
Decoding of a string s:
if
pad_charis notNone, count the number N of contiguous padding characters at the end ofsand strip them, obtainings_strippedif
paddingis"require", ensure that N is exactly the minimum number of padding characters that must be appended tos_strippedto make its length an integral multiple ofblock_ncharsconverts
sto an unsigned integeri, using the encoding alphabet for digits of the encoding basecompute the number
extra_nbits = (char_nbits*len(s))%8of pad bits: if this is not smaller thanchar_nbits, raise bases.encoding.errors.DecodingErrorextract the value
i%(2**extra_nbits)of the pad bits: if this is not zero, raiseDecodingErrorcompute the number of bytes in the decoded bytestring as
original_nbytes = (char_nbits*len(s))//8right-shift
ibyextra_nbitsbits, removing the zero pad bitsconverts
ito its minimal byte representation (big-endian), then zero-pad on the left to reachoriginal_nbytesbytes
FixcharBaseEncoding
- class FixcharBaseEncoding(alphabet, *, case_sensitive=None, char_nbits='auto', pad_char=None, padding='ignore')[source]
Bases:
BaseEncodingFixed-character 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)char_nbits (
intor"auto", optional) – number of bits per character (default:"auto")pad_char (
strorNone, optional) – optional padding character (default:None)padding (
PaddingOptions, optional) – padding style (default:"ignore")
- canonical_bytes(b)[source]
Returns a canonical version of the bytestring
b: this is the bytestring obtained by first encodingband then decoding it.(This method is overridden by subclasses with more efficient implementations.)
- Parameters:
b (
BytesLike) – the bytestring- Return type:
- canonical_string(s)[source]
Returns a canonical version of the string
s: this is the string obtained by first decodingsand then encoding it.(This method is overridden by subclasses with more efficient implementations.)
- property include_padding
Whether padding is included on encoding (derived from
padding).- Return type:
- nopad(allow=True)[source]
Returns a copy of this encoding which does not include/require paddding (and optionally disallows it by removing the padding character).
Example usage:
>>> encoding.base32 FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='include') >>> encoding.base32.nopad() FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=') >>> encoding.base32.nopad(allow=False) FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False))
- Parameters:
allow (
bool) – whether padding is to be allowed on decoding- Return type:
- 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'}
- pad(require=False)[source]
Returns a copy of this encoding which includes paddding (and optionally requires it).
Example usage, from
"include"to"require":>>> encoding.base32 FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='include') >>> encoding.base32.pad(require=True) FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='require')
Example usage, from
"ignore"to"include":>>> encoding.base32z FixcharBaseEncoding( StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False)) >>> encoding.base32z.with_pad_char("=") FixcharBaseEncoding( StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False), pad_char='=') >>> encoding.base32z.with_pad_char("=").pad() FixcharBaseEncoding( StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False), pad_char='=', padding='include')
- Parameters:
require (
bool) – whether padding is to be required on decoding- Return type:
- property pad_char
An optional character to be used for padding of encoded strings. In rfc4648, this is
"="for both base64 and base32.
- pad_string(s)[source]
If no padding character is specified for this encoding, returns the input string unchanged. If a padding character is specified for this encoding, pads the input string by appending the minimum number of padding characters necessary to make its length an integral multiple of the block char size (given by
block_nchars).The value of
paddingis irrelevant to this method.
- property padding
Padding style:
"ignore": no padding included on encoding, no padding required on decoding"include": padding included on encoding, no padding required on decoding"require": padding included on encoding, padding required on decoding
- Return type:
- property require_padding
Whether padding is required on decoding (derived from
padding).- Return type:
- strip_string(s)[source]
If no padding character is specified for this encoding, returns the input string unchanged. If a padding character is specified for this encoding, strips the input string of any padding characters it might have to the right. If
paddingis set to"require", checks that the correct number of padding characters were included and raisesPaddingErrorif not.
- with_pad_char(pad_char)[source]
Returns a copy of this encoding with a different padding character (or without a padding character if pad_char is None).
Example usage:
>>> encoding.base32 FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='include') >>> encoding.base32.with_pad_char("~") FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='~', padding='include')
- Parameters:
- Return type:
PaddingOptions
- PaddingOptions
Type of allowed padding options for fixed-character encoding. See
FixcharBaseEncoding.padding.alias of
Literal[‘ignore’, ‘include’, ‘require’]