Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

multiformats/py-multibase

Repository files navigation

py-multibase

Multibase implementation for Python

Multibase is a protocol for distinguishing base encodings and other simple string encodings, and for ensuring full compatibility with program interfaces.

It answers the question: Given data d encoded into string s, how can I tell what base d is encoded with?

Base encodings exist because transports have restrictions, use special in-band sequences, or must be human-friendly. When systems chose a base to use, it is not always clear which base to use, as there are many tradeoffs in the decision. Multibase is here to save programs and programmers from worrying about which encoding is best.

It solves the biggest problem: a program can use multibase to take input or produce output in whichever base is desired.

The important part is that the value is self-describing, letting other programs elsewhere know what encoding it is using.

Installation

$ pip install py-multibase

Sample Usage

>> encode('base64', 'hello world') b'mGhlbGxvIHdvcmxk' >>> # decoding a multibase >>> decode('mGhlbGxvIHdvcmxk') b'hello world' >>> decode(b'zStV1DL6CwTryKyV') b'hello world' >>> decode(encode('base2', b'hello world')) b'hello world' >>> # Using reusable Encoder/Decoder classes >>> from multibase import Encoder, Decoder >>> encoder = Encoder('base64') >>> encoded1 = encoder.encode('data1') >>> encoded2 = encoder.encode('data2') >>> decoder = Decoder() >>> decoded = decoder.decode(encoded1) >>> # Getting encoding information >>> from multibase import get_encoding_info, list_encodings, is_encoding_supported >>> info = get_encoding_info('base64') >>> print(info.encoding, info.code) base64 b'm' >>> all_encodings = list_encodings() >>> is_encoding_supported('base64') True >>> # Decode with encoding return >>> encoding, data = decode(encoded1, return_encoding=True) >>> print(f'Encoded with {encoding}: {data}')">>>> # encoding a buffer
>>> from multibase import encode, decode
>>> encode('base58btc', 'hello world')
b'zStV1DL6CwTryKyV'
>>> encode('base64', 'hello world')
b'mGhlbGxvIHdvcmxk'
>>> # decoding a multibase
>>> decode('mGhlbGxvIHdvcmxk')
b'hello world'
>>> decode(b'zStV1DL6CwTryKyV')
b'hello world'
>>> decode(encode('base2', b'hello world'))
b'hello world'

>>> # Using reusable Encoder/Decoder classes
>>> from multibase import Encoder, Decoder
>>> encoder = Encoder('base64')
>>> encoded1 = encoder.encode('data1')
>>> encoded2 = encoder.encode('data2')

>>> decoder = Decoder()
>>> decoded = decoder.decode(encoded1)

>>> # Getting encoding information
>>> from multibase import get_encoding_info, list_encodings, is_encoding_supported
>>> info = get_encoding_info('base64')
>>> print(info.encoding, info.code)
base64 b'm'
>>> all_encodings = list_encodings()
>>> is_encoding_supported('base64')
True

>>> # Decode with encoding return
>>> encoding, data = decode(encoded1, return_encoding=True)
>>> print(f'Encoded with {encoding}: {data}')

Supported codecs

  • base2
  • base8
  • base10
  • base16
  • base16upper
  • base32hex
  • base32hexupper
  • base32hexpad
  • base32hexpadupper
  • base32
  • base32upper
  • base32pad
  • base32padupper
  • base32z
  • base36
  • base36upper
  • base58flickr
  • base58btc
  • base64
  • base64pad
  • base64url
  • base64urlpad
  • base256emoji

About

Multibase implementation in Python

Resources

Readme

License

MIT license

Contributing

Contributing

Stars

Watchers

Forks

Packages

Contributors