Multibase
The multibase
module implements the multibase spec.
>>> from multiformats import multibase
Multibase
The Multibase
class provides a container for multibase encoding data:
>>> from multiformats.multibase import Multibase
>>> Multibase(name="base16", code="f",
status="default", description="hexadecimal")
Multibase(name='base16', code='f',
status='default', description='hexadecimal')
encode, decode
The encode
and decode
functions can be used to encode a bytestring into a string using a chosen multibase encoding and to decode a string into a bytestring using the multibase encoding specified by its first character:
>>> multibase.encode(b"Hello World!", "base32")
'bjbswy3dpeblw64tmmqqq'
>>> multibase.decode('bjbswy3dpeblw64tmmqqq')
b'Hello World!'
Multibase
objects have encode
and decode
methods that perform functionality analogous to the homonymous functions:
>>> base32 = multibase.get("base32")
>>> base32.encode(b"Hello World!")
'bjbswy3dpeblw64tmmqqq'
>>> base32.decode('bjbswy3dpeblw64tmmqqq')
b'Hello World!'
The decode
method includes additional encoding validation:
>>> base32.decode('Bjbswy3dpeblw64tmmqqq')
err.ValueError: Expected 'base32' encoding,
found 'base32upper' encoding instead.
from_str
The multibase encoding specified by a given string is accessible using the from_str
function:
>>> multibase.from_str('bjbswy3dpeblw64tmmqqq')
Multibase(encoding='base32', code='b',
status='default',
description='rfc4648 case-insensitive - no padding')
get, exists
Additional encoding management functionality is provided by the exists
and get
functions,
which can be used to check whether an encoding with given name or code is known, and if so to get the corresponding object:
>>> multibase.exists("base32")
True
>>> multibase.get("base32")
Multibase(encoding='base32', code='b',
status='default',
description='rfc4648 case-insensitive - no padding')
>>> multibase.exists(code="f")
True
>>> multibase.get(code="f")
Multibase(encoding="base16", code="f",
status="default", description="hexadecimal")
table
The table
function can be used to iterate through known multibase encodings:
>>> list(enc.name for enc in multibase.table())
['identity', 'base2', 'base8', 'base10', 'base32upper',
'base32padupper', 'base16upper', 'base36upper', 'base64pad',
'base32hexpadupper', 'base64urlpad', 'base32hexupper',
'base58flickr', 'base32', 'base32pad', 'base16', 'base32z',
'base36', 'base64', 'proquint', 'base32hexpad', 'base64url',
'base32hex', 'base58btc']