bases.encoding
Module containing classes for encodings.
BaseEncodingKind
base10
- base10 = ZeropadBaseEncoding(StringAlphabet('0123456789'))
Base-10 encoding.
base16
base2
- base2 = ZeropadBaseEncoding(StringAlphabet('01'), block_nchars=8)
Base-2 encoding (8-char blocks).
base32
base32hex
base32z
- base32z = FixcharBaseEncoding(StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False))
Lowercase case-insensitive human-oriented base-32 encoding from https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
base36
- base36 = ZeropadBaseEncoding(StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', case_sensitive=False))
Uppercase case-insensitive base-36 alphabet.
base45
- base45 = BlockBaseEncoding(StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:', case_sensitive=False), block_size={1: 2, 2: 3}, reverse_blocks=True)
Uppercase case-insensitive base-45 encoding from https://datatracker.ietf.org/doc/draft-faltstrom-base45/
Note that this is (slightly) different from the ISO/IEC 18004:2015 standard.
base58btc
- base58btc = ZeropadBaseEncoding(StringAlphabet('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'))
Case-sensitive base-58 encoding used by Bitcoin.
Specs from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02
base58flickr
- base58flickr = ZeropadBaseEncoding(StringAlphabet('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'))
Case-sensitive base-58 encoding used by Flickr.
Spec from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02 but using alphabet
base58flickr
.
base58ripple
- base58ripple = ZeropadBaseEncoding(StringAlphabet('rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'))
Case-sensitive base-58 encoding used by Ripple.
Spec from https://datatracker.ietf.org/doc/html/draft-msporny-base58-02 but using alphabet
base58ripple
.
base64
base64url
base8
- base8 = FixcharBaseEncoding(StringAlphabet('01234567'), pad_char='=', padding='include')
Base-8 encoding from https://github.com/multiformats/multibase/blob/master/rfcs/Base8.md
get
- get(name)[source]
Gets an encoding by name.
Example usage:
>>> bases.get("base16") ZeropadBaseEncoding( StringAlphabet('0123456789ABCDEF', case_sensitive=False), block_nchars=2)
has
make
- make(chars, *, kind, name=None, case_sensitive=None, **kwargs)[source]
Utility function to create custom encodings.
The
kind
keyword argument can be used to select different encoding constructors:if
"simple-enc"
, usesSimpleBaseEncoding
if
"zeropad-enc"
, usesZeropadBaseEncoding
if
"block-enc"
, usesBlockBaseEncoding
if
"fixchar-enc"
, usesFixcharBaseEncoding
If the optional keyword argument
name
is specified, the encoding is automatically registered usingregister
.The positional argument
chars
and the keyword argumentcase_sensitive
are common to all encodings (withBlockBaseEncoding
additionally accepting aBaseEncoding
instance). The additionalkwargs
are those of each individual class constructor.Example usage:
>>> from bases import encoding >>> encoding.make("0123", kind="zeropad-enc", block_nchars=4, name="base4") ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4) >>> encoding.get("base4") ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4) >>> encoding.get("base4").encode(b"0x7E") '0300132003131011'
- Parameters:
chars (
str
,range
,Alphabet
orBaseEncoding
) – the alphabet to use for the encoding, or a “block encoding” when'kind'
is set to'block-enc'
kind (
"simple-enc"
,"zeropad-enc"
,"block-enc"
or"fixchar-enc"
) – whether the alphabet is case-sensitivename (
str
orNone
, optional) – if specified, the newly created alphabet is registered with this namecase_sensitive (
bool
, optional) – whether the encoding alphabet is case-sensitivekwargs (
Dict
[str
,Any
]) – additional keyword arguments to be passed to the chosen encoding constructor
- Return type:
register
- register(**encs)[source]
Registers any number of new encodings by name.
Example usage:
>>> bases.register(base16lower=encoding.base16.lower()) >>> bases.get("base16lower") ZeropadBaseEncoding( StringAlphabet('0123456789abcdef', case_sensitive=False), block_nchars=2)
Encoding names must conform with:
re.match(r"^base[0-9][a-zA-Z0-9_]*$", name)
- Parameters:
encs (
Dict
[str
,BaseEncoding
]) – the encodings to register, passed by desired registration name- Raises:
KeyError – if an encoding with one of the given names already exists
- Return type:
table
- table(*, prefix='')[source]
Iterates over all registered encodings, optionally restricting to those with given prefix:
Example usage:
>>> from bases import encoding >>> dict(encoding.table(prefix="base32")) {'base32': FixcharBaseEncoding( StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False), pad_char='=', padding='include'), 'base32hex': FixcharBaseEncoding( StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV', case_sensitive=False), pad_char='=', padding='include'), 'base32z': FixcharBaseEncoding( StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False)) }
unregister
- unregister(*names)[source]
Unregisters any number of existing encodings by name.
Example usage:
>>> from bases import encoding >>> encoding.unregister("base16", "base32") >>> encoding.has("base16") False >>> encoding.get("base16") KeyError: "Encoding named 'base16' does not exist."
Note that pre-defined constants are unaffected by this:
>>> encoding.base16 ZeropadBaseEncoding( StringAlphabet('0123456789ABCDEF', case_sensitive=False), block_nchars=2)