py-multibase

2.0.0 · active · verified Thu Apr 16

py-multibase is a Python library that provides an implementation of the Multibase protocol. Multibase is designed to distinguish base encodings (like base64, base58btc, etc.) and other simple string encodings, ensuring compatibility across different systems by making the encoding self-describing. The library is currently at version 2.0.0 and is actively maintained, facilitating encoding and decoding of data with various Multibase formats.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode and decode data using various multibase formats, including `base58btc` and `base64`. It also shows the use of reusable `Encoder` and `Decoder` classes for multiple operations and how to list supported encodings. Remember that `encode` takes `str` or `bytes` and `decode` returns `bytes`, so you might need to convert to `str` for printing.

from multibase import encode, decode, list_encodings

# Encode a string to base58btc
data_to_encode = 'hello world'
encoded_base58 = encode('base58btc', data_to_encode)
print(f"Encoded (base58btc): {encoded_base58}")

# Encode a string to base64
encoded_base64 = encode('base64', data_to_encode)
print(f"Encoded (base64): {encoded_base64}")

# Decode a multibase string
decoded_data = decode(encoded_base64)
print(f"Decoded: {decoded_data.decode('utf-8')}")

# Using reusable Encoder/Decoder classes
from multibase import Encoder, Decoder
base64_encoder = Encoder('base64')
encoded_reusable = base64_encoder.encode(b'reusable data')
print(f"Encoded (reusable base64): {encoded_reusable}")

decoder = Decoder()
decoded_reusable = decoder.decode(encoded_reusable)
print(f"Decoded (reusable): {decoded_reusable.decode('utf-8')}")

# List available encodings
# print(list_encodings())

view raw JSON →