├── docs ├── _static │ └── .gitkeep ├── usage.rst ├── authors.rst ├── history.rst ├── contributing.rst ├── .gitignore ├── api_reference.rst ├── index.rst ├── installation.rst ├── Makefile ├── make.bat └── conf.py ├── tests ├── __init__.py └── test_multibase.py ├── codecov.yaml ├── AUTHORS.rst ├── newsfragments ├── 18.internal.rst ├── README.md ├── validate_files.py └── 20.feature.rst ├── MANIFEST.in ├── .readthedocs.yaml ├── .editorconfig ├── .github ├── ISSUE_TEMPLATE.md └── workflows │ └── tox.yml ├── HISTORY.rst ├── tox.ini ├── ruff.toml ├── multibase ├── exceptions.py ├── __init__.py ├── converters.py └── multibase.py ├── .pre-commit-config.yaml ├── .gitignore ├── LICENSE ├── Makefile ├── README.rst ├── travis_pypi_setup.py ├── pyproject.toml └── CONTRIBUTING.rst /docs/_static/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | Usage 2 | ----- 3 | -------------------------------------------------------------------------------- /docs/authors.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../AUTHORS.rst 2 | -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst 2 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst 2 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | """Unit test package for multibase.""" 2 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | /multibase.rst 2 | /multibase.*.rst 3 | /modules.rst 4 | -------------------------------------------------------------------------------- /codecov.yaml: -------------------------------------------------------------------------------- 1 | comment: 2 | layout: header, changes, diff, uncovered 3 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | Contributors 2 | ------------ 3 | 4 | * @cardoso-neto 5 | * @dhruvbaldawa 6 | * @pinnaculum 7 | -------------------------------------------------------------------------------- /docs/api_reference.rst: -------------------------------------------------------------------------------- 1 | API Reference 2 | ------------- 3 | 4 | .. py:currentmodule:: multibase 5 | 6 | .. autofunction:: encode 7 | 8 | .. autofunction:: decode 9 | 10 | .. autofunction:: get_codec 11 | 12 | .. autofunction:: is_encoded 13 | -------------------------------------------------------------------------------- /newsfragments/18.internal.rst: -------------------------------------------------------------------------------- 1 | Modernized project infrastructure: migrated to pyproject.toml, replaced Travis CI with GitHub Actions, updated Python support to 3.10-3.14, replaced flake8 with ruff, and added pre-commit hooks. This is an internal change that does not affect the public API. 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS.rst 2 | include CONTRIBUTING.rst 3 | include HISTORY.rst 4 | include LICENSE 5 | include README.rst 6 | 7 | recursive-include tests * 8 | recursive-exclude * __pycache__ 9 | recursive-exclude * *.py[co] 10 | 11 | recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif 12 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | build: 4 | os: ubuntu-24.04 5 | tools: 6 | python: "3.10" 7 | 8 | sphinx: 9 | configuration: docs/conf.py 10 | fail_on_warning: true 11 | 12 | python: 13 | install: 14 | - method: pip 15 | path: . 16 | extra_requirements: 17 | - dev 18 | 19 | formats: 20 | - epub 21 | - htmlzip 22 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | charset = utf-8 11 | end_of_line = lf 12 | 13 | [*.bat] 14 | indent_style = tab 15 | end_of_line = crlf 16 | 17 | [LICENSE] 18 | insert_final_newline = false 19 | 20 | [Makefile] 21 | indent_style = tab 22 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | 3 | 4 | Table of contents 5 | ================= 6 | 7 | .. toctree:: 8 | :maxdepth: 2 9 | 10 | installation 11 | usage 12 | api_reference 13 | modules 14 | contributing 15 | authors 16 | history 17 | 18 | Indices and tables 19 | ================== 20 | 21 | * :ref:`genindex` 22 | * :ref:`modindex` 23 | * :ref:`search` 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | * py-multibase version: 2 | * Python version: 3 | * Operating System: 4 | 5 | ### Description 6 | 7 | Describe what you were trying to get done. 8 | Tell us what happened, what went wrong, and what you expected to happen. 9 | 10 | ### What I Did 11 | 12 | ``` 13 | Paste the command(s) you ran and the output. 14 | If there was a crash, please include the traceback here. 15 | ``` 16 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | History 2 | ------- 3 | 4 | 1.0.3 (2020-10-26) 5 | ================== 6 | * Add base36 and base36 upper encodings 7 | * Fix issue with base16 encoding 8 | 9 | 1.0.0 (2018-10-19) 10 | ================== 11 | 12 | * Re-implement encoding for base32 and base64, as the implementation was buggy 13 | * Add extensive tests for all encodings 14 | 15 | 0.1.0 (2017-09-02) 16 | ================== 17 | 18 | * First release on PyPI. 19 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist= 3 | py{310,311,312,313,314}-core 4 | py{310,311,312,313,314}-lint 5 | docs 6 | 7 | [testenv] 8 | extras= 9 | dev 10 | commands= 11 | core: pytest {posargs:tests} 12 | 13 | [testenv:py{310,311,312,313,314}-lint] 14 | deps=pre-commit 15 | commands= 16 | ; pre-commit install 17 | pre-commit run --all-files --show-diff-on-failure 18 | 19 | [testenv:docs] 20 | commands = 21 | make docs-ci 22 | allowlist_externals= 23 | make 24 | -------------------------------------------------------------------------------- /ruff.toml: -------------------------------------------------------------------------------- 1 | line-length = 120 2 | indent-width = 4 3 | exclude = [ 4 | ".eggs", 5 | ".git", 6 | ".ruff_cache", 7 | ".tox", 8 | ".venv", 9 | "venv", 10 | "build", 11 | "dist", 12 | "docs", 13 | "*.egg", 14 | ] 15 | 16 | [lint] 17 | select = ["E", "F", "W", "I", "RUF"] 18 | dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" 19 | 20 | [format] 21 | quote-style = "double" 22 | indent-style = "space" 23 | skip-magic-trailing-comma = false 24 | line-ending = "lf" 25 | -------------------------------------------------------------------------------- /multibase/exceptions.py: -------------------------------------------------------------------------------- 1 | """Custom exceptions for multibase encoding/decoding errors.""" 2 | 3 | 4 | class MultibaseError(ValueError): 5 | """Base exception for all multibase errors.""" 6 | 7 | pass 8 | 9 | 10 | class UnsupportedEncodingError(MultibaseError): 11 | """Raised when an encoding is not supported.""" 12 | 13 | pass 14 | 15 | 16 | class InvalidMultibaseStringError(MultibaseError): 17 | """Raised when a multibase string is invalid or cannot be decoded.""" 18 | 19 | pass 20 | 21 | 22 | class DecodingError(MultibaseError): 23 | """Raised when decoding fails.""" 24 | 25 | pass 26 | -------------------------------------------------------------------------------- /multibase/__init__.py: -------------------------------------------------------------------------------- 1 | """Top-level package for py-multibase.""" 2 | 3 | __author__ = """Dhruv Baldawa""" 4 | __email__ = "dhruv@dhruvb.com" 5 | __version__ = "1.0.3" 6 | 7 | from .exceptions import ( # noqa: F401 8 | DecodingError, 9 | InvalidMultibaseStringError, 10 | MultibaseError, 11 | UnsupportedEncodingError, 12 | ) 13 | from .multibase import ( # noqa: F401 14 | ENCODINGS, 15 | ComposedDecoder, 16 | Decoder, 17 | Encoder, 18 | Encoding, 19 | decode, 20 | encode, 21 | get_codec, 22 | get_encoding_info, 23 | is_encoded, 24 | is_encoding_supported, 25 | list_encodings, 26 | ) 27 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | exclude: '.project-template|docs/conf.py|.*pb2\..*' 2 | repos: 3 | - repo: https://github.com/pre-commit/pre-commit-hooks 4 | rev: v5.0.0 5 | hooks: 6 | - id: check-yaml 7 | - id: check-toml 8 | - id: end-of-file-fixer 9 | - id: trailing-whitespace 10 | - repo: https://github.com/asottile/pyupgrade 11 | rev: v3.15.0 12 | hooks: 13 | - id: pyupgrade 14 | args: [--py310-plus] 15 | - repo: https://github.com/astral-sh/ruff-pre-commit 16 | rev: v0.11.10 17 | hooks: 18 | - id: ruff 19 | args: [--fix, --exit-non-zero-on-fix] 20 | - id: ruff-format 21 | - repo: local 22 | hooks: 23 | - id: mypy-local 24 | name: run mypy with all dev dependencies present 25 | entry: mypy -p multibase 26 | language: system 27 | always_run: true 28 | pass_filenames: false 29 | require_serial: false 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | venv/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | 56 | # Sphinx documentation 57 | docs/_build/ 58 | 59 | # PyBuilder 60 | target/ 61 | 62 | # pyenv python configuration file 63 | .python-version 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | 4 | Copyright (c) 2017, Dhruv Baldawa 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /newsfragments/README.md: -------------------------------------------------------------------------------- 1 | This directory collects "newsfragments": short files that each contain 2 | a snippet of ReST-formatted text that will be added to the next 3 | release notes. This should be a description of aspects of the change 4 | (if any) that are relevant to users. (This contrasts with the 5 | commit message and PR description, which are a description of the change as 6 | relevant to people working on the code itself.) 7 | 8 | Each file should be named like `..rst`, where 9 | `` is an issue number, and `` is one of: 10 | 11 | - `breaking` 12 | - `bugfix` 13 | - `deprecation` 14 | - `docs` 15 | - `feature` 16 | - `internal` 17 | - `misc` 18 | - `performance` 19 | - `removal` 20 | 21 | So for example: `123.feature.rst`, `456.bugfix.rst` 22 | 23 | Files should be placed directly in this `newsfragments/` directory (not in subdirectories). 24 | 25 | If the PR fixes an issue, use that number here. If there is no issue, 26 | then open up the PR first and use the PR number for the newsfragment. 27 | 28 | Note that the `towncrier` tool will automatically 29 | reflow your text, so don't try to do any fancy formatting. Run 30 | `towncrier build --draft` to get a preview of what the release notes entry 31 | will look like in the final release notes. 32 | -------------------------------------------------------------------------------- /newsfragments/validate_files.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Towncrier silently ignores files that do not match the expected ending. 4 | # We use this script to ensure we catch these as errors in CI. 5 | 6 | import pathlib 7 | import sys 8 | 9 | ALLOWED_EXTENSIONS = { 10 | ".breaking.rst", 11 | ".bugfix.rst", 12 | ".deprecation.rst", 13 | ".docs.rst", 14 | ".feature.rst", 15 | ".internal.rst", 16 | ".misc.rst", 17 | ".performance.rst", 18 | ".removal.rst", 19 | } 20 | 21 | ALLOWED_FILES = { 22 | "validate_files.py", 23 | "README.md", 24 | } 25 | 26 | THIS_DIR = pathlib.Path(__file__).parent 27 | 28 | num_args = len(sys.argv) - 1 29 | assert num_args in {0, 1} 30 | if num_args == 1: 31 | assert sys.argv[1] in ("is-empty",) 32 | 33 | for fragment_file in THIS_DIR.iterdir(): 34 | if fragment_file.name in ALLOWED_FILES: 35 | continue 36 | elif num_args == 0: 37 | full_extension = "".join(fragment_file.suffixes) 38 | if full_extension not in ALLOWED_EXTENSIONS: 39 | raise Exception(f"Unexpected file: {fragment_file}") 40 | elif sys.argv[1] == "is-empty": 41 | raise Exception(f"Unexpected file: {fragment_file}") 42 | else: 43 | raise RuntimeError(f"Strange: arguments {sys.argv} were validated, but not found") 44 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | 8 | Stable release 9 | -------------- 10 | 11 | To install py-multibase, run this command in your terminal: 12 | 13 | .. code-block:: console 14 | 15 | $ pip install multibase 16 | 17 | This is the preferred method to install py-multibase, as it will always install the most recent stable release. 18 | 19 | If you don't have `pip`_ installed, this `Python installation guide`_ can guide 20 | you through the process. 21 | 22 | .. _pip: https://pip.pypa.io 23 | .. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ 24 | 25 | 26 | From sources 27 | ------------ 28 | 29 | The sources for py-multibase can be downloaded from the `Github repo`_. 30 | 31 | You can either clone the public repository: 32 | 33 | .. code-block:: console 34 | 35 | $ git clone git://github.com/multiformats/py-multibase 36 | 37 | Or download the `tarball`_: 38 | 39 | .. code-block:: console 40 | 41 | $ curl -OL https://github.com/multiformats/py-multibase/tarball/master 42 | 43 | Once you have a copy of the source, you can install it with: 44 | 45 | .. code-block:: console 46 | 47 | $ pip install -e . 48 | 49 | 50 | .. _Github repo: https://github.com/multiformats/py-multibase 51 | .. _tarball: https://github.com/multiformats/py-multibase/tarball/master 52 | -------------------------------------------------------------------------------- /newsfragments/20.feature.rst: -------------------------------------------------------------------------------- 1 | Added complete multibase encoding support and enhanced API features. 2 | 3 | **New Encodings (10 total):** 4 | - base16upper (prefix F) - Uppercase hexadecimal encoding 5 | - base32upper (prefix B) - Uppercase base32 encoding 6 | - base32pad (prefix c) - Base32 with RFC 4648 padding 7 | - base32padupper (prefix C) - Base32 uppercase with padding 8 | - base32hexupper (prefix V) - Base32hex uppercase variant 9 | - base32hexpad (prefix t) - Base32hex with RFC 4648 padding 10 | - base32hexpadupper (prefix T) - Base32hex uppercase with padding 11 | - base64pad (prefix M) - Base64 with RFC 4648 padding 12 | - base64urlpad (prefix U) - Base64url with padding 13 | - base256emoji (prefix 🚀) - Emoji-based encoding 14 | 15 | **API Enhancements:** 16 | - Added ``Encoder`` and ``Decoder`` classes for reusable encoding/decoding 17 | - Added ``decode(return_encoding=True)`` parameter to return encoding type along with decoded data 18 | - Added structured exception classes: ``UnsupportedEncodingError``, ``InvalidMultibaseStringError``, ``DecodingError`` 19 | - Added encoding metadata functions: ``get_encoding_info()``, ``list_encodings()``, ``is_encoding_supported()`` 20 | - Added decoder composition support via ``Decoder.or_()`` method 21 | 22 | This brings py-multibase to 100% encoding coverage (24/24 encodings) matching reference implementations (go-multibase, rust-multibase, js-multiformats). 23 | -------------------------------------------------------------------------------- /.github/workflows/tox.yml: -------------------------------------------------------------------------------- 1 | name: Run tox 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | 9 | defaults: 10 | run: 11 | shell: bash 12 | 13 | jobs: 14 | tox: 15 | runs-on: ubuntu-latest 16 | strategy: 17 | matrix: 18 | python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] 19 | toxenv: [core, lint] 20 | include: 21 | - python-version: "3.10" 22 | toxenv: docs 23 | fail-fast: false 24 | steps: 25 | - uses: actions/checkout@v4 26 | - uses: actions/setup-python@v5 27 | with: 28 | python-version: ${{ matrix.python-version }} 29 | - name: Set TOXENV 30 | run: | 31 | if [[ "${{ matrix.toxenv }}" == "docs" ]]; then 32 | echo "TOXENV=docs" >> "$GITHUB_ENV" 33 | else 34 | python_version="${{ matrix.python-version }}" 35 | echo "TOXENV=py${python_version//./}-${{ matrix.toxenv }}" >> "$GITHUB_ENV" 36 | fi 37 | - run: | 38 | python -m pip install --upgrade pip 39 | python -m pip install "tox>=4.10" 40 | - run: | 41 | python -m tox run -e "$TOXENV" -r 42 | 43 | windows: 44 | runs-on: windows-latest 45 | strategy: 46 | matrix: 47 | python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] 48 | toxenv: [core, lint] 49 | fail-fast: false 50 | steps: 51 | - uses: actions/checkout@v4 52 | - name: Set up Python ${{ matrix.python-version }} 53 | uses: actions/setup-python@v5 54 | with: 55 | python-version: ${{ matrix.python-version }} 56 | - name: Set TOXENV 57 | shell: bash 58 | run: | 59 | python_version="${{ matrix.python-version }}" 60 | echo "TOXENV=py${python_version//./}-${{ matrix.toxenv }}" >> "$GITHUB_ENV" 61 | - name: Install dependencies 62 | run: | 63 | python -m pip install --upgrade pip 64 | python -m pip install "tox>=4.10" 65 | - name: Test with tox 66 | shell: bash 67 | run: | 68 | python -m tox run -e "$TOXENV" -r 69 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean-pyc clean-build docs clean help pr 2 | define BROWSER_PYSCRIPT 3 | import os, webbrowser, sys 4 | try: 5 | from urllib import pathname2url 6 | except: 7 | from urllib.request import pathname2url 8 | 9 | webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) 10 | endef 11 | export BROWSER_PYSCRIPT 12 | BROWSER := python -c "$$BROWSER_PYSCRIPT" 13 | 14 | help: 15 | @echo "Available commands:" 16 | @echo "clean-build - remove build artifacts" 17 | @echo "clean-pyc - remove Python file artifacts" 18 | @echo "clean-test - remove test artifacts" 19 | @echo "clean - run clean-build, clean-pyc, and clean-test" 20 | @echo "setup - install development requirements" 21 | @echo "fix - fix formatting & linting issues with ruff" 22 | @echo "lint - run pre-commit hooks on all files" 23 | @echo "typecheck - run mypy type checking" 24 | @echo "test - run tests quickly with the default Python" 25 | @echo "coverage - run tests with coverage report" 26 | @echo "docs-ci - generate docs for CI" 27 | @echo "docs - generate docs and open in browser" 28 | @echo "servedocs - serve docs with live reload" 29 | @echo "dist - build package and show contents" 30 | @echo "pr - run clean, lint, and test (everything needed before creating a PR)" 31 | 32 | clean: clean-build clean-pyc clean-test 33 | 34 | clean-build: 35 | rm -fr build/ 36 | rm -fr dist/ 37 | rm -fr .eggs/ 38 | find . -name '*.egg-info' -exec rm -fr {} + 39 | find . -name '*.egg' -exec rm -rf {} + 40 | 41 | clean-pyc: 42 | find . -name '*.pyc' -exec rm -f {} + 43 | find . -name '*.pyo' -exec rm -f {} + 44 | find . -name '*~' -exec rm -f {} + 45 | find . -name '__pycache__' -exec rm -fr {} + 46 | 47 | clean-test: 48 | rm -fr .tox/ 49 | rm -fr .mypy_cache 50 | rm -fr .ruff_cache 51 | rm -f .coverage 52 | rm -fr htmlcov/ 53 | 54 | setup: 55 | pip install -e ".[dev]" 56 | 57 | lint: 58 | pre-commit run --all-files 59 | 60 | fix: 61 | python -m ruff check --fix 62 | 63 | typecheck: 64 | pre-commit run mypy-local --all-files 65 | 66 | test: 67 | python -m pytest tests 68 | 69 | coverage: 70 | coverage run --source multibase -m pytest tests 71 | coverage report -m 72 | coverage html 73 | $(BROWSER) htmlcov/index.html 74 | 75 | docs-ci: 76 | rm -f docs/multibase.rst 77 | rm -f docs/modules.rst 78 | sphinx-apidoc -o docs/ multibase 79 | $(MAKE) -C docs clean 80 | mkdir -p docs/_static 81 | $(MAKE) -C docs html SPHINXOPTS="-W" 82 | 83 | docs: docs-ci 84 | $(BROWSER) docs/_build/html/index.html 85 | 86 | servedocs: docs 87 | watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . 88 | 89 | dist: clean 90 | python -m build 91 | ls -l dist 92 | 93 | pr: clean fix lint typecheck test 94 | @echo "PR preparation complete! All checks passed." 95 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | **This project is no longer maintained and has been archived.** 2 | 3 | py-multibase 4 | ------------ 5 | 6 | .. image:: https://img.shields.io/pypi/v/py-multibase.svg 7 | :target: https://pypi.python.org/pypi/py-multibase 8 | 9 | .. image:: https://github.com/multiformats/py-multibase/actions/workflows/tox.yml/badge.svg 10 | :target: https://github.com/multiformats/py-multibase/actions 11 | 12 | .. image:: https://codecov.io/gh/multiformats/py-multibase/branch/master/graph/badge.svg 13 | :target: https://codecov.io/gh/multiformats/py-multibase 14 | 15 | .. image:: https://readthedocs.org/projects/py-multibase/badge/?version=stable 16 | :target: https://py-multibase.readthedocs.io/en/stable/?badge=stable 17 | :alt: Documentation Status 18 | 19 | `Multibase `_ implementation for Python 20 | 21 | Multibase is a protocol for distinguishing base encodings and other simple string encodings, and for ensuring full compatibility with program interfaces. 22 | 23 | It answers the question: Given data d encoded into string s, how can I tell what base d is encoded with? 24 | 25 | Base encodings exist because transports have restrictions, use special in-band sequences, or must be human-friendly. 26 | When systems chose a base to use, it is not always clear which base to use, as there are many tradeoffs in the decision. 27 | Multibase is here to save programs and programmers from worrying about which encoding is best. 28 | 29 | It solves the biggest problem: a program can use multibase to take input or produce output in whichever base is desired. 30 | 31 | The important part is that the value is self-describing, letting other programs elsewhere know what encoding it is using. 32 | 33 | * Free software: MIT license 34 | * Documentation: https://py-multibase.readthedocs.io. 35 | * Python versions: Python 3.10, 3.11, 3.12, 3.13, 3.14 36 | 37 | Installation 38 | ============ 39 | 40 | .. code-block:: shell 41 | 42 | $ pip install py-multibase 43 | 44 | 45 | Sample Usage 46 | ============ 47 | 48 | .. code-block:: python 49 | 50 | >>> # encoding a buffer 51 | >>> from multibase import encode, decode 52 | >>> encode('base58btc', 'hello world') 53 | b'zStV1DL6CwTryKyV' 54 | >>> encode('base64', 'hello world') 55 | b'mGhlbGxvIHdvcmxk' 56 | >>> # decoding a multibase 57 | >>> decode('mGhlbGxvIHdvcmxk') 58 | b'hello world' 59 | >>> decode(b'zStV1DL6CwTryKyV') 60 | b'hello world' 61 | >>> decode(encode('base2', b'hello world')) 62 | b'hello world' 63 | 64 | >>> # Using reusable Encoder/Decoder classes 65 | >>> from multibase import Encoder, Decoder 66 | >>> encoder = Encoder('base64') 67 | >>> encoded1 = encoder.encode('data1') 68 | >>> encoded2 = encoder.encode('data2') 69 | 70 | >>> decoder = Decoder() 71 | >>> decoded = decoder.decode(encoded1) 72 | 73 | >>> # Getting encoding information 74 | >>> from multibase import get_encoding_info, list_encodings, is_encoding_supported 75 | >>> info = get_encoding_info('base64') 76 | >>> print(info.encoding, info.code) 77 | base64 b'm' 78 | >>> all_encodings = list_encodings() 79 | >>> is_encoding_supported('base64') 80 | True 81 | 82 | >>> # Decode with encoding return 83 | >>> encoding, data = decode(encoded1, return_encoding=True) 84 | >>> print(f'Encoded with {encoding}: {data}') 85 | 86 | 87 | Supported codecs 88 | ================ 89 | 90 | * base2 91 | * base8 92 | * base10 93 | * base16 94 | * base16upper 95 | * base32hex 96 | * base32hexupper 97 | * base32hexpad 98 | * base32hexpadupper 99 | * base32 100 | * base32upper 101 | * base32pad 102 | * base32padupper 103 | * base32z 104 | * base36 105 | * base36upper 106 | * base58flickr 107 | * base58btc 108 | * base64 109 | * base64pad 110 | * base64url 111 | * base64urlpad 112 | * base256emoji 113 | -------------------------------------------------------------------------------- /travis_pypi_setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Update encrypted deploy password in Travis config file.""" 3 | 4 | import base64 5 | import json 6 | import os 7 | from getpass import getpass 8 | 9 | import yaml 10 | from cryptography.hazmat.backends import default_backend 11 | from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 12 | from cryptography.hazmat.primitives.serialization import load_pem_public_key 13 | 14 | try: 15 | from urllib import urlopen 16 | except ImportError: 17 | from urllib.request import urlopen 18 | 19 | 20 | GITHUB_REPO = "multiformats/py-multibase" 21 | TRAVIS_CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".travis.yml") 22 | 23 | 24 | def load_key(pubkey): 25 | """Load public RSA key. 26 | 27 | Work around keys with incorrect header/footer format. 28 | 29 | Read more about RSA encryption with cryptography: 30 | https://cryptography.io/latest/hazmat/primitives/asymmetric/rsa/ 31 | """ 32 | try: 33 | return load_pem_public_key(pubkey.encode(), default_backend()) 34 | except ValueError: 35 | # workaround for https://github.com/travis-ci/travis-api/issues/196 36 | pubkey = pubkey.replace("BEGIN RSA", "BEGIN").replace("END RSA", "END") 37 | return load_pem_public_key(pubkey.encode(), default_backend()) 38 | 39 | 40 | def encrypt(pubkey, password): 41 | """Encrypt password using given RSA public key and encode it with base64. 42 | 43 | The encrypted password can only be decrypted by someone with the 44 | private key (in this case, only Travis). 45 | """ 46 | key = load_key(pubkey) 47 | encrypted_password = key.encrypt(password, PKCS1v15()) 48 | return base64.b64encode(encrypted_password) 49 | 50 | 51 | def fetch_public_key(repo): 52 | """Download RSA public key Travis will use for this repo. 53 | 54 | Travis API docs: http://docs.travis-ci.com/api/#repository-keys 55 | """ 56 | keyurl = f"https://api.travis-ci.org/repos/{repo}/key" 57 | data = json.loads(urlopen(keyurl).read().decode()) 58 | if "key" not in data: 59 | errmsg = f"Could not find public key for repo: {repo}.\n" 60 | errmsg += "Have you already added your GitHub repo to Travis?" 61 | raise ValueError(errmsg) 62 | return data["key"] 63 | 64 | 65 | def prepend_line(filepath, line): 66 | """Rewrite a file adding a line to its beginning.""" 67 | with open(filepath) as f: 68 | lines = f.readlines() 69 | 70 | lines.insert(0, line) 71 | 72 | with open(filepath, "w") as f: 73 | f.writelines(lines) 74 | 75 | 76 | def load_yaml_config(filepath): 77 | """Load yaml config file at the given path.""" 78 | with open(filepath) as f: 79 | return yaml.load(f) 80 | 81 | 82 | def save_yaml_config(filepath, config): 83 | """Save yaml config file at the given path.""" 84 | with open(filepath, "w") as f: 85 | yaml.dump(config, f, default_flow_style=False) 86 | 87 | 88 | def update_travis_deploy_password(encrypted_password): 89 | """Put `encrypted_password` into the deploy section of .travis.yml.""" 90 | config = load_yaml_config(TRAVIS_CONFIG_FILE) 91 | 92 | config["deploy"]["password"] = dict(secure=encrypted_password) 93 | 94 | save_yaml_config(TRAVIS_CONFIG_FILE, config) 95 | 96 | line = "# This file was autogenerated and will overwrite each time you run travis_pypi_setup.py\n" 97 | prepend_line(TRAVIS_CONFIG_FILE, line) 98 | 99 | 100 | def main(args): 101 | """Add a PyPI password to .travis.yml so that Travis can deploy to PyPI. 102 | 103 | Fetch the Travis public key for the repo, and encrypt the PyPI password 104 | with it before adding, so that only Travis can decrypt and use the PyPI 105 | password. 106 | """ 107 | public_key = fetch_public_key(args.repo) 108 | password = args.password or getpass("PyPI password: ") 109 | update_travis_deploy_password(encrypt(public_key, password.encode())) 110 | print("Wrote encrypted password to .travis.yml -- you're ready to deploy") 111 | 112 | 113 | if "__main__" == __name__: 114 | import argparse 115 | 116 | parser = argparse.ArgumentParser(description=__doc__) 117 | parser.add_argument("--repo", default=GITHUB_REPO, help="GitHub repo (default: %s)" % GITHUB_REPO) 118 | parser.add_argument("--password", help="PyPI password (will prompt if not provided)") 119 | 120 | args = parser.parse_args() 121 | main(args) 122 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=42", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "py-multibase" 7 | version = "1.0.3" 8 | description = "Multibase implementation for Python" 9 | readme = "README.rst" 10 | authors = [{ name = "Dhruv Baldawa", email = "dhruv@dhruvb.com" }] 11 | license = { text = "MIT" } 12 | keywords = ["multibase"] 13 | classifiers = [ 14 | "Development Status :: 2 - Pre-Alpha", 15 | "Intended Audience :: Developers", 16 | "License :: OSI Approved :: MIT License", 17 | "Natural Language :: English", 18 | "Programming Language :: Python :: 3", 19 | "Programming Language :: Python :: 3.10", 20 | "Programming Language :: Python :: 3.11", 21 | "Programming Language :: Python :: 3.12", 22 | "Programming Language :: Python :: 3.13", 23 | "Programming Language :: Python :: 3.14", 24 | ] 25 | requires-python = ">=3.10, <4.0" 26 | dependencies = [ 27 | "python-baseconv>=1.2.0,<2.0", 28 | "six>=1.10.0,<2.0", 29 | "morphys>=1.0,<2.0", 30 | ] 31 | 32 | [project.urls] 33 | Homepage = "https://github.com/multiformats/py-multibase" 34 | Download = "https://github.com/multiformats/py-multibase/tarball/1.0.3" 35 | 36 | [project.optional-dependencies] 37 | dev = [ 38 | "Sphinx>=5.0.0", 39 | "build>=0.9.0", 40 | "bump-my-version>=1.2.0", 41 | "codecov", 42 | "coverage>=6.5.0", 43 | "mypy", 44 | "pre-commit", 45 | "pytest", 46 | "pytest-cov", 47 | "pytest-runner", 48 | "ruff", 49 | "towncrier>=24,<25", 50 | "tox>=4.10.0", 51 | "twine", 52 | "watchdog>=3.0.0", 53 | "wheel>=0.31.0", 54 | ] 55 | 56 | [tool.setuptools] 57 | include-package-data = true 58 | zip-safe = false 59 | 60 | [tool.setuptools.packages.find] 61 | where = ["."] 62 | include = ["multibase*"] 63 | 64 | [tool.pytest.ini_options] 65 | testpaths = ["tests"] 66 | python_classes = "*TestCase" 67 | 68 | [tool.coverage.run] 69 | source = ["multibase"] 70 | 71 | [tool.coverage.report] 72 | exclude_lines = [ 73 | "pragma: no cover", 74 | "def __repr__", 75 | "if self.debug:", 76 | "if settings.DEBUG", 77 | "raise AssertionError", 78 | "raise NotImplementedError", 79 | "if 0:", 80 | "if __name__ == .__main__.:", 81 | ] 82 | 83 | [tool.towncrier] 84 | # Read https://github.com/multiformats/py-multibase/blob/master/newsfragments/README.md for instructions 85 | directory = "newsfragments" 86 | filename = "HISTORY.rst" 87 | issue_format = "`#{issue} `__" 88 | package = "multibase" 89 | title_format = "py-multibase v{version} ({project_date})" 90 | underlines = ["-", "~", "^"] 91 | ignore = ["validate_files.py", "README.md"] 92 | 93 | [[tool.towncrier.type]] 94 | directory = "breaking" 95 | name = "Breaking Changes" 96 | showcontent = true 97 | 98 | [[tool.towncrier.type]] 99 | directory = "bugfix" 100 | name = "Bugfixes" 101 | showcontent = true 102 | 103 | [[tool.towncrier.type]] 104 | directory = "deprecation" 105 | name = "Deprecations" 106 | showcontent = true 107 | 108 | [[tool.towncrier.type]] 109 | directory = "docs" 110 | name = "Improved Documentation" 111 | showcontent = true 112 | 113 | [[tool.towncrier.type]] 114 | directory = "feature" 115 | name = "Features" 116 | showcontent = true 117 | 118 | [[tool.towncrier.type]] 119 | directory = "internal" 120 | name = "Internal Changes - for py-multibase Contributors" 121 | showcontent = true 122 | 123 | [[tool.towncrier.type]] 124 | directory = "misc" 125 | name = "Miscellaneous Changes" 126 | showcontent = false 127 | 128 | [[tool.towncrier.type]] 129 | directory = "performance" 130 | name = "Performance Improvements" 131 | showcontent = true 132 | 133 | [[tool.towncrier.type]] 134 | directory = "removal" 135 | name = "Removals" 136 | showcontent = true 137 | 138 | [tool.bumpversion] 139 | current_version = "1.0.3" 140 | parse = """ 141 | (?P\\d+) 142 | \\.(?P\\d+) 143 | \\.(?P\\d+) 144 | """ 145 | serialize = [ 146 | "{major}.{minor}.{patch}", 147 | ] 148 | search = "{current_version}" 149 | replace = "{new_version}" 150 | regex = false 151 | ignore_missing_version = false 152 | tag = true 153 | sign_tags = true 154 | tag_name = "v{new_version}" 155 | tag_message = "Bump version: {current_version} → {new_version}" 156 | allow_dirty = false 157 | commit = true 158 | message = "Bump version: {current_version} → {new_version}" 159 | 160 | [[tool.bumpversion.files]] 161 | filename = "pyproject.toml" 162 | search = "version = \"{current_version}\"" 163 | replace = "version = \"{new_version}\"" 164 | 165 | [[tool.bumpversion.files]] 166 | filename = "multibase/__init__.py" 167 | search = "__version__ = '{current_version}'" 168 | replace = "__version__ = '{new_version}'" 169 | 170 | [[tool.bumpversion.files]] 171 | filename = "pyproject.toml" 172 | search = "Download = \"https://github.com/multiformats/py-multibase/tarball/{current_version}\"" 173 | replace = "Download = \"https://github.com/multiformats/py-multibase/tarball/{new_version}\"" 174 | 175 | [tool.mypy] 176 | python_version = "3.10" 177 | check_untyped_defs = false 178 | disallow_any_generics = false 179 | disallow_incomplete_defs = false 180 | disallow_subclassing_any = false 181 | disallow_untyped_calls = false 182 | disallow_untyped_decorators = false 183 | disallow_untyped_defs = false 184 | ignore_missing_imports = true 185 | incremental = false 186 | strict_equality = false 187 | strict_optional = false 188 | warn_redundant_casts = false 189 | warn_return_any = false 190 | warn_unused_configs = true 191 | warn_unused_ignores = false 192 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Contributing 5 | ============ 6 | 7 | Contributions are welcome, and they are greatly appreciated! Every 8 | little bit helps, and credit will always be given. 9 | 10 | You can contribute in many ways: 11 | 12 | Types of Contributions 13 | ---------------------- 14 | 15 | Report Bugs 16 | ~~~~~~~~~~~ 17 | 18 | Report bugs at https://github.com/multiformats/py-multibase/issues. 19 | 20 | If you are reporting a bug, please include: 21 | 22 | * Your operating system name and version. 23 | * Any details about your local setup that might be helpful in troubleshooting. 24 | * Detailed steps to reproduce the bug. 25 | 26 | Fix Bugs 27 | ~~~~~~~~ 28 | 29 | Look through the GitHub issues for bugs. Anything tagged with "bug" 30 | and "help wanted" is open to whoever wants to implement it. 31 | 32 | Implement Features 33 | ~~~~~~~~~~~~~~~~~~ 34 | 35 | Look through the GitHub issues for features. Anything tagged with "enhancement" 36 | and "help wanted" is open to whoever wants to implement it. 37 | 38 | Write Documentation 39 | ~~~~~~~~~~~~~~~~~~~ 40 | 41 | py-multibase could always use more documentation, whether as part of the 42 | official py-multibase docs, in docstrings, or even on the web in blog posts, 43 | articles, and such. 44 | 45 | Submit Feedback 46 | ~~~~~~~~~~~~~~~ 47 | 48 | The best way to send feedback is to file an issue at https://github.com/multiformats/py-multibase/issues. 49 | 50 | If you are proposing a feature: 51 | 52 | * Explain in detail how it would work. 53 | * Keep the scope as narrow as possible, to make it easier to implement. 54 | * Remember that this is a volunteer-driven project, and that contributions 55 | are welcome :) 56 | 57 | Get Started! 58 | ------------ 59 | 60 | Ready to contribute? Here's how to set up `multibase` for local development. 61 | 62 | 1. Fork the `py-multibase` repo on GitHub. 63 | 2. Clone your fork locally:: 64 | 65 | $ git clone git@github.com:your_name_here/py-multibase.git 66 | 67 | 3. Install your local copy into a virtualenv. Create and activate a virtual environment:: 68 | 69 | $ python -m venv venv 70 | $ source venv/bin/activate # On Windows: venv\Scripts\activate 71 | $ pip install -e ".[dev]" 72 | 73 | 4. Install pre-commit hooks (optional but recommended):: 74 | 75 | $ pre-commit install 76 | 77 | This will set up git hooks to automatically run linting and formatting checks 78 | before each commit. 79 | 80 | 5. Create a branch for local development:: 81 | 82 | $ git checkout -b name-of-your-bugfix-or-feature 83 | 84 | Now you can make your changes locally. 85 | 86 | 6. When you're done making changes, check that your changes pass linting and the 87 | tests, including testing other Python versions with tox:: 88 | 89 | $ make lint 90 | $ make test 91 | $ tox 92 | 93 | Or run pre-commit manually on all files:: 94 | 95 | $ pre-commit run --all-files 96 | 97 | If you installed pre-commit hooks (step 4), they will run automatically on commit. 98 | 99 | Development Workflow Commands 100 | ------------------------------- 101 | 102 | The project provides several ``make`` targets to help with development: 103 | 104 | * ``make fix`` - Automatically fix formatting and linting issues using ruff. 105 | Use this when you want to auto-fix code style issues. 106 | 107 | * ``make lint`` - Run all pre-commit hooks on all files to check for code quality 108 | issues. This includes YAML/TOML validation, trailing whitespace checks, pyupgrade, 109 | ruff linting and formatting, and mypy type checking. 110 | 111 | * ``make typecheck`` - Run mypy type checking only. Use this when you want to 112 | quickly check for type errors without running all other checks. 113 | 114 | * ``make test`` - Run the test suite with pytest using the default Python version. 115 | For testing across multiple Python versions, use ``tox`` instead. 116 | 117 | * ``make pr`` - Run a complete pre-PR check: clean build artifacts, fix formatting, 118 | run linting, type checking, and tests. This is the recommended command to run 119 | before submitting a pull request. 120 | 121 | * ``make coverage`` - Run tests with coverage reporting and open the HTML report 122 | in your browser. 123 | 124 | For a full list of available commands, run ``make help``. 125 | 126 | 7. Commit your changes and push your branch to GitHub:: 127 | 128 | $ git add . 129 | $ git commit -m "Your detailed description of your changes." 130 | $ git push -u origin name-of-your-bugfix-or-feature 131 | 132 | 8. Submit a pull request through the GitHub website. 133 | 134 | Pull Request Guidelines 135 | ----------------------- 136 | 137 | Before you submit a pull request, check that it meets these guidelines: 138 | 139 | 1. The pull request should include tests. 140 | 2. If the pull request adds functionality, the docs should be updated. Put 141 | your new functionality into a function with a docstring, and add the 142 | feature to the list in README.rst. 143 | 3. The pull request should work for Python 3.10, 3.11, 3.12, 3.13, and 3.14. Check 144 | https://github.com/multiformats/py-multibase/actions 145 | and make sure that the tests pass for all supported Python versions. 146 | 147 | Tips 148 | ---- 149 | 150 | To run a subset of tests:: 151 | 152 | $ pytest tests/test_multibase.py 153 | 154 | 155 | Deploying 156 | --------- 157 | 158 | A reminder for the maintainers on how to deploy. 159 | Make sure all your changes are committed (including an entry in HISTORY.rst). 160 | Then run:: 161 | 162 | $ bump-my-version bump patch # possible: major / minor / patch 163 | $ git push 164 | $ git push --tags 165 | 166 | GitHub Actions will then deploy to PyPI if tests pass. 167 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/multibase.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/multibase.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/multibase" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/multibase" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\multibase.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\multibase.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /tests/test_multibase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """Tests for `multibase` package.""" 4 | 5 | import pytest 6 | from morphys import ensure_bytes 7 | 8 | from multibase import ( 9 | Decoder, 10 | DecodingError, 11 | Encoder, 12 | InvalidMultibaseStringError, 13 | UnsupportedEncodingError, 14 | decode, 15 | encode, 16 | get_encoding_info, 17 | is_encoded, 18 | is_encoding_supported, 19 | list_encodings, 20 | ) 21 | 22 | TEST_FIXTURES = ( 23 | ("identity", "yes mani !", "\x00yes mani !"), 24 | ("base2", "yes mani !", "01111001011001010111001100100000011011010110000101101110011010010010000000100001"), 25 | ("base8", "yes mani !", "7171312714403326055632220041"), 26 | ("base10", "yes mani !", "9573277761329450583662625"), 27 | ("base16", "yes mani !", "f796573206d616e692021"), 28 | ("base16", "\x01", "f01"), 29 | ("base16", "\x0f", "f0f"), 30 | ("base16", "f", "f66"), 31 | ("base16", "fo", "f666f"), 32 | ("base16", "foo", "f666f6f"), 33 | ("base16", "foob", "f666f6f62"), 34 | ("base16", "fooba", "f666f6f6261"), 35 | ("base16", "foobar", "f666f6f626172"), 36 | ("base16upper", "yes mani !", "F796573206D616E692021"), 37 | ("base16upper", "f", "F66"), 38 | ("base16upper", "fo", "F666F"), 39 | ("base16upper", "foo", "F666F6F"), 40 | ("base32", "yes mani !", "bpfsxgidnmfxgsibb"), 41 | ("base32", "f", "bmy"), 42 | ("base32", "fo", "bmzxq"), 43 | ("base32", "foo", "bmzxw6"), 44 | ("base32", "foob", "bmzxw6yq"), 45 | ("base32", "fooba", "bmzxw6ytb"), 46 | ("base32", "foobar", "bmzxw6ytboi"), 47 | ("base32upper", "yes mani !", "BPFSXGIDNMFXGSIBB"), 48 | ("base32upper", "f", "BMY"), 49 | ("base32upper", "fo", "BMZXQ"), 50 | ("base32upper", "foo", "BMZXW6"), 51 | ("base32pad", "yes mani !", "cpfsxgidnmfxgsibb"), 52 | ("base32pad", "f", "cmy======"), 53 | ("base32pad", "fo", "cmzxq===="), 54 | ("base32pad", "foo", "cmzxw6==="), 55 | ("base32pad", "foob", "cmzxw6yq="), 56 | ("base32pad", "fooba", "cmzxw6ytb"), 57 | ("base32pad", "foobar", "cmzxw6ytboi======"), 58 | ("base32padupper", "yes mani !", "CPFSXGIDNMFXGSIBB"), 59 | ("base32padupper", "f", "CMY======"), 60 | ("base32padupper", "fo", "CMZXQ===="), 61 | ("base32padupper", "foo", "CMZXW6==="), 62 | ("base32hex", "yes mani !", "vf5in683dc5n6i811"), 63 | ("base32hex", "f", "vco"), 64 | ("base32hex", "fo", "vcpng"), 65 | ("base32hex", "foo", "vcpnmu"), 66 | ("base32hex", "foob", "vcpnmuog"), 67 | ("base32hex", "fooba", "vcpnmuoj1"), 68 | ("base32hex", "foobar", "vcpnmuoj1e8"), 69 | ("base32hexupper", "yes mani !", "VF5IN683DC5N6I811"), 70 | ("base32hexupper", "f", "VCO"), 71 | ("base32hexupper", "fo", "VCPNG"), 72 | ("base32hexpad", "yes mani !", "tf5in683dc5n6i811"), 73 | ("base32hexpad", "f", "tco======"), 74 | ("base32hexpad", "fo", "tcpng===="), 75 | ("base32hexpad", "foo", "tcpnmu==="), 76 | ("base32hexpad", "foob", "tcpnmuog="), 77 | ("base32hexpad", "fooba", "tcpnmuoj1"), 78 | ("base32hexpad", "foobar", "tcpnmuoj1e8======"), 79 | ("base32hexpadupper", "yes mani !", "TF5IN683DC5N6I811"), 80 | ("base32hexpadupper", "f", "TCO======"), 81 | ("base32hexpadupper", "fo", "TCPNG===="), 82 | ("base32z", "yes mani !", "hxf1zgedpcfzg1ebb"), 83 | ("base36", "Decentralize everything!!!", "km552ng4dabi4neu1oo8l4i5mndwmpc3mkukwtxy9"), 84 | ("base36upper", "Decentralize everything!!!", "KM552NG4DABI4NEU1OO8L4I5MNDWMPC3MKUKWTXY9"), 85 | ("base58flickr", "yes mani !", "Z7Pznk19XTTzBtx"), 86 | ("base58btc", "yes mani !", "z7paNL19xttacUY"), 87 | ("base64", "÷ïÿ", "mw7fDr8O/"), 88 | ("base64", "f", "mZg"), 89 | ("base64", "fo", "mZm8"), 90 | ("base64", "foo", "mZm9v"), 91 | ("base64", "foob", "mZm9vYg"), 92 | ("base64", "fooba", "mZm9vYmE"), 93 | ("base64", "foobar", "mZm9vYmFy"), 94 | ("base64pad", "f", "MZg=="), 95 | ("base64pad", "fo", "MZm8="), 96 | ("base64pad", "foo", "MZm9v"), 97 | ("base64pad", "foob", "MZm9vYg=="), 98 | ("base64pad", "fooba", "MZm9vYmE="), 99 | ("base64pad", "foobar", "MZm9vYmFy"), 100 | ("base64url", "÷ïÿ", "uw7fDr8O_"), 101 | ("base64urlpad", "f", "UZg=="), 102 | ("base64urlpad", "fo", "UZm8="), 103 | ("base64urlpad", "foo", "UZm9v"), 104 | ("base64urlpad", "foob", "UZm9vYg=="), 105 | ("base64urlpad", "fooba", "UZm9vYmE="), 106 | ("base64urlpad", "foobar", "UZm9vYmFy"), 107 | ) 108 | 109 | 110 | INCORRECT_ENCODINGS = ("base58", "base4") 111 | INCORRECT_ENCODED_DATA = ("abcdefghi", "!qweqweqeqw") 112 | 113 | 114 | @pytest.mark.parametrize("encoding,data,encoded_data", TEST_FIXTURES) 115 | def test_encode(encoding, data, encoded_data): 116 | assert encode(encoding, data) == ensure_bytes(encoded_data) 117 | 118 | 119 | @pytest.mark.parametrize("encoding", INCORRECT_ENCODINGS) 120 | def test_encode_incorrect_encoding(encoding): 121 | with pytest.raises(UnsupportedEncodingError) as excinfo: 122 | encode(encoding, "test data") 123 | assert "not supported" in str(excinfo.value) 124 | 125 | 126 | @pytest.mark.parametrize("_,data,encoded_data", TEST_FIXTURES) 127 | def test_decode(_, data, encoded_data): 128 | assert decode(encoded_data) == ensure_bytes(data) 129 | 130 | 131 | @pytest.mark.parametrize("encoded_data", INCORRECT_ENCODED_DATA) 132 | def test_decode_incorrect_encoding(encoded_data): 133 | with pytest.raises(InvalidMultibaseStringError) as excinfo: 134 | decode(encoded_data) 135 | assert "Can not determine encoding" in str(excinfo.value) 136 | 137 | 138 | @pytest.mark.parametrize("_,data,encoded_data", TEST_FIXTURES) 139 | def test_is_encoded(_, data, encoded_data): 140 | assert is_encoded(encoded_data) 141 | 142 | 143 | @pytest.mark.parametrize("encoded_data", INCORRECT_ENCODED_DATA) 144 | def test_is_encoded_incorrect_encoding(encoded_data): 145 | assert not is_encoded(encoded_data) 146 | 147 | 148 | def test_decode_return_encoding(): 149 | """Test decode with return_encoding parameter.""" 150 | encoding, decoded = decode("f796573206d616e692021", return_encoding=True) 151 | assert encoding == "base16" 152 | assert decoded == ensure_bytes("yes mani !") 153 | 154 | 155 | def test_is_encoding_supported(): 156 | """Test is_encoding_supported function.""" 157 | assert is_encoding_supported("base64") 158 | assert is_encoding_supported("base16") 159 | assert not is_encoding_supported("base999") 160 | 161 | 162 | def test_list_encodings(): 163 | """Test list_encodings function.""" 164 | encodings = list_encodings() 165 | assert "base64" in encodings 166 | assert "base16" in encodings 167 | assert "base32pad" in encodings 168 | assert "base64pad" in encodings 169 | assert len(encodings) >= 24 # Should have at least 24 encodings 170 | 171 | 172 | def test_get_encoding_info(): 173 | """Test get_encoding_info function.""" 174 | info = get_encoding_info("base64") 175 | assert info.encoding == "base64" 176 | assert info.code == b"m" 177 | assert info.converter is not None 178 | 179 | with pytest.raises(UnsupportedEncodingError): 180 | get_encoding_info("base999") 181 | 182 | 183 | def test_encoder_class(): 184 | """Test Encoder class.""" 185 | encoder = Encoder("base64") 186 | assert encoder.encoding == "base64" 187 | 188 | encoded = encoder.encode("hello") 189 | assert encoded.startswith(b"m") 190 | 191 | with pytest.raises(UnsupportedEncodingError): 192 | Encoder("base999") 193 | 194 | 195 | def test_decoder_class(): 196 | """Test Decoder class.""" 197 | decoder = Decoder() 198 | # Use a known good encoding 199 | test_data = encode("base64", "hello") 200 | decoded = decoder.decode(test_data) 201 | assert decoded == ensure_bytes("hello") 202 | 203 | encoding, decoded = decoder.decode(test_data, return_encoding=True) 204 | assert encoding == "base64" 205 | assert decoded == ensure_bytes("hello") 206 | 207 | 208 | def test_decoder_composition(): 209 | """Test decoder composition with or_ method.""" 210 | decoder1 = Decoder() 211 | decoder2 = Decoder() 212 | 213 | # This should work with any valid multibase string 214 | composed = decoder1.or_(decoder2) 215 | test_data = encode("base64", "hello") 216 | decoded = composed.decode(test_data) 217 | assert decoded == ensure_bytes("hello") 218 | 219 | # Should fail with invalid data 220 | with pytest.raises(DecodingError): 221 | composed.decode("invalid") 222 | 223 | 224 | def test_composed_decoder_all_fail(): 225 | """Test ComposedDecoder error message when all decoders fail.""" 226 | decoder1 = Decoder() 227 | decoder2 = Decoder() 228 | composed = decoder1.or_(decoder2) 229 | 230 | with pytest.raises(DecodingError) as excinfo: 231 | composed.decode("invalid") 232 | assert "All decoders failed" in str(excinfo.value) 233 | assert "Last error" in str(excinfo.value) 234 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # multibase documentation build configuration file, created by 5 | # sphinx-quickstart on Tue Jul 9 22:26:36 2013. 6 | # 7 | # This file is execfile()d with the current directory set to its 8 | # containing dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | import sys 17 | import os 18 | 19 | # If extensions (or modules to document with autodoc) are in another 20 | # directory, add these directories to sys.path here. If the directory is 21 | # relative to the documentation root, use os.path.abspath to make it 22 | # absolute, like shown here. 23 | #sys.path.insert(0, os.path.abspath('.')) 24 | 25 | # Get the project root dir, which is the parent dir of this 26 | cwd = os.getcwd() 27 | project_root = os.path.dirname(cwd) 28 | 29 | # Insert the project root dir as the first element in the PYTHONPATH. 30 | # This lets us ensure that the source package is imported, and that its 31 | # version is used. 32 | sys.path.insert(0, project_root) 33 | 34 | import multibase 35 | 36 | # -- General configuration --------------------------------------------- 37 | 38 | # If your documentation needs a minimal Sphinx version, state it here. 39 | #needs_sphinx = '1.0' 40 | 41 | # Add any Sphinx extension module names here, as strings. They can be 42 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 43 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 44 | 45 | # Add any paths that contain templates here, relative to this directory. 46 | templates_path = ['_templates'] 47 | 48 | # The suffix of source filenames. 49 | source_suffix = '.rst' 50 | 51 | # The encoding of source files. 52 | #source_encoding = 'utf-8-sig' 53 | 54 | # The master toctree document. 55 | master_doc = 'index' 56 | 57 | # General information about the project. 58 | project = u'py-multibase' 59 | copyright = u"2017, Dhruv Baldawa" 60 | 61 | # The version info for the project you're documenting, acts as replacement 62 | # for |version| and |release|, also used in various other places throughout 63 | # the built documents. 64 | # 65 | # The short X.Y version. 66 | version = multibase.__version__ 67 | # The full version, including alpha/beta/rc tags. 68 | release = multibase.__version__ 69 | 70 | # The language for content autogenerated by Sphinx. Refer to documentation 71 | # for a list of supported languages. 72 | #language = None 73 | 74 | # There are two options for replacing |today|: either, you set today to 75 | # some non-false value, then it is used: 76 | #today = '' 77 | # Else, today_fmt is used as the format for a strftime call. 78 | #today_fmt = '%B %d, %Y' 79 | 80 | # List of patterns, relative to source directory, that match files and 81 | # directories to ignore when looking for source files. 82 | exclude_patterns = ['_build'] 83 | 84 | # The reST default role (used for this markup: `text`) to use for all 85 | # documents. 86 | #default_role = None 87 | 88 | # If true, '()' will be appended to :func: etc. cross-reference text. 89 | #add_function_parentheses = True 90 | 91 | # If true, the current module name will be prepended to all description 92 | # unit titles (such as .. function::). 93 | #add_module_names = True 94 | 95 | # If true, sectionauthor and moduleauthor directives will be shown in the 96 | # output. They are ignored by default. 97 | #show_authors = False 98 | 99 | # The name of the Pygments (syntax highlighting) style to use. 100 | pygments_style = 'sphinx' 101 | 102 | # A list of ignored prefixes for module index sorting. 103 | #modindex_common_prefix = [] 104 | 105 | # If true, keep warnings as "system message" paragraphs in the built 106 | # documents. 107 | #keep_warnings = False 108 | 109 | 110 | # -- Options for HTML output ------------------------------------------- 111 | 112 | # The theme to use for HTML and HTML Help pages. See the documentation for 113 | # a list of builtin themes. 114 | html_theme = 'alabaster' 115 | 116 | # Theme options are theme-specific and customize the look and feel of a 117 | # theme further. For a list of options available for each theme, see the 118 | # documentation. 119 | #html_theme_options = {} 120 | 121 | # Add any paths that contain custom themes here, relative to this directory. 122 | #html_theme_path = [] 123 | 124 | # The name for this set of Sphinx documents. If None, it defaults to 125 | # " v documentation". 126 | #html_title = None 127 | 128 | # A shorter title for the navigation bar. Default is the same as 129 | # html_title. 130 | #html_short_title = None 131 | 132 | # The name of an image file (relative to this directory) to place at the 133 | # top of the sidebar. 134 | #html_logo = None 135 | 136 | # The name of an image file (within the static path) to use as favicon 137 | # of the docs. This file should be a Windows icon file (.ico) being 138 | # 16x16 or 32x32 pixels large. 139 | #html_favicon = None 140 | 141 | # Add any paths that contain custom static files (such as style sheets) 142 | # here, relative to this directory. They are copied after the builtin 143 | # static files, so a file named "default.css" will overwrite the builtin 144 | # "default.css". 145 | html_static_path = ['_static'] 146 | 147 | # If not '', a 'Last updated on:' timestamp is inserted at every page 148 | # bottom, using the given strftime format. 149 | #html_last_updated_fmt = '%b %d, %Y' 150 | 151 | # If true, SmartyPants will be used to convert quotes and dashes to 152 | # typographically correct entities. 153 | #html_use_smartypants = True 154 | 155 | # Custom sidebar templates, maps document names to template names. 156 | #html_sidebars = {} 157 | 158 | # Additional templates that should be rendered to pages, maps page names 159 | # to template names. 160 | #html_additional_pages = {} 161 | 162 | # If false, no module index is generated. 163 | #html_domain_indices = True 164 | 165 | # If false, no index is generated. 166 | #html_use_index = True 167 | 168 | # If true, the index is split into individual pages for each letter. 169 | #html_split_index = False 170 | 171 | # If true, links to the reST sources are added to the pages. 172 | #html_show_sourcelink = True 173 | 174 | # If true, "Created using Sphinx" is shown in the HTML footer. 175 | # Default is True. 176 | #html_show_sphinx = True 177 | 178 | # If true, "(C) Copyright ..." is shown in the HTML footer. 179 | # Default is True. 180 | #html_show_copyright = True 181 | 182 | # If true, an OpenSearch description file will be output, and all pages 183 | # will contain a tag referring to it. The value of this option 184 | # must be the base URL from which the finished HTML is served. 185 | #html_use_opensearch = '' 186 | 187 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 188 | #html_file_suffix = None 189 | 190 | # Output file base name for HTML help builder. 191 | htmlhelp_basename = 'multibasedoc' 192 | 193 | html_sidebars = { 194 | '**': [ 195 | 'about.html', 196 | 'navigation.html', 197 | 'relations.html', 198 | 'searchbox.html', 199 | ], 200 | } 201 | 202 | html_theme_options = { 203 | 'github_user': 'dhruvbaldawa', 204 | 'github_repo': 'py-multibase', 205 | 'github_button': True, 206 | 'github_banner': True, 207 | 'code_font_size': '0.8em', 208 | } 209 | 210 | 211 | # -- Options for LaTeX output ------------------------------------------ 212 | 213 | latex_elements = { 214 | # The paper size ('letterpaper' or 'a4paper'). 215 | #'papersize': 'letterpaper', 216 | 217 | # The font size ('10pt', '11pt' or '12pt'). 218 | #'pointsize': '10pt', 219 | 220 | # Additional stuff for the LaTeX preamble. 221 | #'preamble': '', 222 | } 223 | 224 | # Grouping the document tree into LaTeX files. List of tuples 225 | # (source start file, target name, title, author, documentclass 226 | # [howto/manual]). 227 | latex_documents = [ 228 | ('index', 'multibase.tex', 229 | u'py-multibase Documentation', 230 | u'Dhruv Baldawa', 'manual'), 231 | ] 232 | 233 | # The name of an image file (relative to this directory) to place at 234 | # the top of the title page. 235 | #latex_logo = None 236 | 237 | # For "manual" documents, if this is true, then toplevel headings 238 | # are parts, not chapters. 239 | #latex_use_parts = False 240 | 241 | # If true, show page references after internal links. 242 | #latex_show_pagerefs = False 243 | 244 | # If true, show URL addresses after external links. 245 | #latex_show_urls = False 246 | 247 | # Documents to append as an appendix to all manuals. 248 | #latex_appendices = [] 249 | 250 | # If false, no module index is generated. 251 | #latex_domain_indices = True 252 | 253 | 254 | # -- Options for manual page output ------------------------------------ 255 | 256 | # One entry per manual page. List of tuples 257 | # (source start file, name, description, authors, manual section). 258 | man_pages = [ 259 | ('index', 'multibase', 260 | u'py-multibase Documentation', 261 | [u'Dhruv Baldawa'], 1) 262 | ] 263 | 264 | # If true, show URL addresses after external links. 265 | #man_show_urls = False 266 | 267 | 268 | # -- Options for Texinfo output ---------------------------------------- 269 | 270 | # Grouping the document tree into Texinfo files. List of tuples 271 | # (source start file, target name, title, author, 272 | # dir menu entry, description, category) 273 | texinfo_documents = [ 274 | ('index', 'multibase', 275 | u'py-multibase Documentation', 276 | u'Dhruv Baldawa', 277 | 'multibase', 278 | 'One line description of project.', 279 | 'Miscellaneous'), 280 | ] 281 | 282 | # Documents to append as an appendix to all manuals. 283 | #texinfo_appendices = [] 284 | 285 | # If false, no module index is generated. 286 | #texinfo_domain_indices = True 287 | 288 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 289 | #texinfo_show_urls = 'footnote' 290 | 291 | # If true, do not generate a @detailmenu in the "Top" node's menu. 292 | #texinfo_no_detailmenu = False 293 | -------------------------------------------------------------------------------- /multibase/converters.py: -------------------------------------------------------------------------------- 1 | from io import BytesIO 2 | from itertools import zip_longest 3 | 4 | from baseconv import BaseConverter 5 | from morphys import ensure_bytes 6 | 7 | 8 | class BaseStringConverter(BaseConverter): 9 | def encode(self, bytes): 10 | number = int.from_bytes(bytes, byteorder="big", signed=False) 11 | return ensure_bytes(super().encode(number)) 12 | 13 | def bytes_to_int(self, bytes): 14 | length = len(bytes) 15 | base = len(self.digits) 16 | value = 0 17 | 18 | for i, x in enumerate(bytes): 19 | value += self.digits.index(chr(x)) * base ** (length - (i + 1)) 20 | return value 21 | 22 | def decode(self, bytes): 23 | decoded_int = self.bytes_to_int(bytes) 24 | # See https://docs.python.org/3.5/library/stdtypes.html#int.to_bytes for more about the magical expression 25 | # below 26 | decoded_data = decoded_int.to_bytes((decoded_int.bit_length() + 7) // 8, byteorder="big") 27 | return decoded_data 28 | 29 | 30 | class Base16StringConverter(BaseStringConverter): 31 | def __init__(self, digits): 32 | super().__init__(digits) 33 | self.uppercase = digits.isupper() 34 | 35 | def encode(self, bytes): 36 | result = "".join([f"{byte:02x}" for byte in bytes]) 37 | if self.uppercase: 38 | result = result.upper() 39 | return ensure_bytes(result) 40 | 41 | def decode(self, data): 42 | # Base16 decode is case-insensitive, normalize to our digits case 43 | if isinstance(data, bytes): 44 | data_str = data.decode("utf-8") 45 | else: 46 | data_str = data 47 | # Convert to match our digits case 48 | if self.uppercase: 49 | data_str = data_str.upper() 50 | else: 51 | data_str = data_str.lower() 52 | return super().decode(data_str.encode("utf-8")) 53 | 54 | 55 | class BaseByteStringConverter: 56 | ENCODE_GROUP_BYTES = 1 57 | ENCODING_BITS = 1 58 | DECODING_BITS = 1 59 | 60 | def __init__(self, digits, pad=False): 61 | self.digits = digits 62 | self.pad = pad 63 | 64 | def _chunk_with_padding(self, iterable, n, fillvalue=None): 65 | "Collect data into fixed-length chunks or blocks" 66 | # _chunk_with_padding('ABCDEFG', 3, 'x') --> ABC DEF Gxx" 67 | args = [iter(iterable)] * n 68 | return zip_longest(*args, fillvalue=fillvalue) 69 | 70 | def _chunk_without_padding(self, iterable, n): 71 | return map("".join, zip(*[iter(iterable)] * n)) 72 | 73 | def _encode_bytes(self, bytes_, group_bytes, encoding_bits, decoding_bits, output_chars): 74 | buffer = BytesIO(bytes_) 75 | encoded_bytes = BytesIO() 76 | input_length = len(bytes_) 77 | 78 | while True: 79 | byte_ = buffer.read(group_bytes) 80 | if not byte_: 81 | break 82 | 83 | # convert all bytes to a binary format and concatenate them into a 24bit string 84 | binstringfmt = f"{{:0{encoding_bits}b}}" 85 | binstring = "".join([binstringfmt.format(x) for x in byte_]) 86 | # break the 24 bit length string into pieces of 6 bits each and convert them to integer 87 | digits = (int("".join(x), 2) for x in self._chunk_with_padding(binstring, decoding_bits, "0")) 88 | 89 | for digit in digits: 90 | # convert binary representation to an integer 91 | encoded_bytes.write(ensure_bytes(self.digits[digit])) 92 | 93 | result = encoded_bytes.getvalue() 94 | 95 | # Add padding if needed (RFC 4648) 96 | if self.pad: 97 | remainder = input_length % group_bytes 98 | if remainder > 0: 99 | # For partial groups, we need to pad the output 100 | # The padding makes the output length a multiple of output_chars 101 | chars_produced = len(result) 102 | # Calculate padding needed to reach next multiple of output_chars 103 | padding_needed = output_chars - (chars_produced % output_chars) 104 | result += ensure_bytes("=" * padding_needed) 105 | 106 | return result 107 | 108 | def _decode_bytes(self, bytes_, group_bytes, decoding_bits, encoding_bits): 109 | # Remove padding if present 110 | if self.pad: 111 | bytes_ = bytes_.rstrip(b"=") 112 | 113 | buffer = BytesIO() 114 | decoded_bytes = BytesIO() 115 | 116 | for byte_ in bytes_.decode(): 117 | idx = self.digits.index(byte_) 118 | buffer.write(bytes([idx])) 119 | 120 | buffer.seek(0) 121 | while True: 122 | byte_ = buffer.read(group_bytes) 123 | if not byte_: 124 | break 125 | 126 | # convert all bytes to a binary format and concatenate them into a 8, 16, 24bit string 127 | binstringfmt = f"{{:0{decoding_bits}b}}" 128 | binstring = "".join([binstringfmt.format(x) for x in byte_]) 129 | 130 | # break the 24 bit length string into pieces of 8 bits each and convert them to integer 131 | digits = [int("".join(x), 2) for x in self._chunk_without_padding(binstring, encoding_bits)] 132 | 133 | for digit in digits: 134 | decoded_bytes.write(bytes([digit])) 135 | 136 | return decoded_bytes.getvalue() 137 | 138 | def encode(self, bytes): 139 | raise NotImplementedError 140 | 141 | def decode(self, bytes): 142 | return NotImplementedError 143 | 144 | 145 | class Base64StringConverter(BaseByteStringConverter): 146 | def encode(self, bytes): 147 | return self._encode_bytes(ensure_bytes(bytes), 3, 8, 6, 4) 148 | 149 | def decode(self, bytes): 150 | return self._decode_bytes(ensure_bytes(bytes), 4, 6, 8) 151 | 152 | 153 | class Base32StringConverter(BaseByteStringConverter): 154 | def encode(self, bytes): 155 | return self._encode_bytes(ensure_bytes(bytes), 5, 8, 5, 8) 156 | 157 | def decode(self, bytes): 158 | return self._decode_bytes(ensure_bytes(bytes), 8, 5, 8) 159 | 160 | 161 | class Base256EmojiConverter: 162 | """Base256 emoji encoding using 256 unique emoji characters. 163 | 164 | This implementation uses the exact same hardcoded emoji alphabet as 165 | js-multiformats and go-multibase reference implementations to ensure 166 | full compatibility. The alphabet is curated from Unicode emoji frequency 167 | data, excluding modifier-based emojis (such as flags) that are bigger 168 | than one single code point. 169 | """ 170 | 171 | # Hardcoded emoji alphabet matching js-multiformats and go-multibase 172 | # This is the exact same alphabet used in reference implementations 173 | # Source: js-multiformats/src/bases/base256emoji.ts and go-multibase/base256emoji.go 174 | _EMOJI_ALPHABET = ( 175 | "🚀🪐☄🛰🌌" # Space 176 | "🌑🌒🌓🌔🌕🌖🌗🌘" # Moon 177 | "🌍🌏🌎" # Earth 178 | "🐉" # Dragon 179 | "☀" # Sun 180 | "💻🖥💾💿" # Computer 181 | # Rest from Unicode emoji frequency data (most used first) 182 | "😂❤😍🤣😊🙏💕😭😘👍" 183 | "😅👏😁🔥🥰💔💖💙😢🤔" 184 | "😆🙄💪😉☺👌🤗💜😔😎" 185 | "😇🌹🤦🎉💞✌✨🤷😱😌" 186 | "🌸🙌😋💗💚😏💛🙂💓🤩" 187 | "😄😀🖤😃💯🙈👇🎶😒🤭" 188 | "❣😜💋👀😪😑💥🙋😞😩" 189 | "😡🤪👊🥳😥🤤👉💃😳✋" 190 | "😚😝😴🌟😬🙃🍀🌷😻😓" 191 | "⭐✅🥺🌈😈🤘💦✔😣🏃" 192 | "💐☹🎊💘😠☝😕🌺🎂🌻" 193 | "😐🖕💝🙊😹🗣💫💀👑🎵" 194 | "🤞😛🔴😤🌼😫⚽🤙☕🏆" 195 | "🤫👈😮🙆🍻🍃🐶💁😲🌿" 196 | "🧡🎁⚡🌞🎈❌✊👋😰🤨" 197 | "😶🤝🚶💰🍓💢🤟🙁🚨💨" 198 | "🤬✈🎀🍺🤓😙💟🌱😖👶" 199 | "🥴▶➡❓💎💸⬇😨🌚🦋" 200 | "😷🕺⚠🙅😟😵👎🤲🤠🤧" 201 | "📌🔵💅🧐🐾🍒😗🤑🌊🤯" 202 | "🐷☎💧😯💆👆🎤🙇🍑❄" 203 | "🌴💣🐸💌📍🥀🤢👅💡💩" 204 | "👐📸👻🤐🤮🎼🥵🚩🍎🍊" 205 | "👼💍📣🥂" 206 | ) 207 | 208 | def __init__(self): 209 | # Verify alphabet length 210 | if len(self._EMOJI_ALPHABET) != 256: 211 | raise ValueError(f"EMOJI_ALPHABET must contain exactly 256 characters, got {len(self._EMOJI_ALPHABET)}") 212 | # Create mapping from byte value to emoji character 213 | self.byte_to_emoji = {i: self._EMOJI_ALPHABET[i] for i in range(256)} 214 | # Create reverse mapping from emoji character to byte value 215 | # This matches the approach in js-multiformats and go-multibase 216 | self.emoji_to_byte = {emoji: byte for byte, emoji in self.byte_to_emoji.items()} 217 | 218 | def encode(self, bytes_) -> bytes: 219 | """Encode bytes to emoji string. 220 | 221 | :param bytes_: Bytes to encode 222 | :type bytes_: bytes or str 223 | :return: UTF-8 encoded emoji string 224 | :rtype: bytes 225 | """ 226 | bytes_ = ensure_bytes(bytes_) 227 | result = [] 228 | for byte_val in bytes_: 229 | result.append(self.byte_to_emoji[byte_val]) 230 | return "".join(result).encode("utf-8") 231 | 232 | def decode(self, bytes_) -> bytes: 233 | """Decode emoji string to bytes. 234 | 235 | Decodes character-by-character, matching the behavior of js-multiformats 236 | and go-multibase reference implementations. Each emoji in the alphabet 237 | is a single Unicode code point, so we can safely iterate character by 238 | character. 239 | 240 | :param bytes_: UTF-8 encoded emoji string 241 | :type bytes_: bytes or str 242 | :return: Decoded bytes 243 | :rtype: bytes 244 | :raises ValueError: if an invalid emoji character is encountered 245 | """ 246 | bytes_ = ensure_bytes(bytes_, "utf8") 247 | # Decode UTF-8 to get emoji string 248 | emoji_str = bytes_.decode("utf-8") 249 | result = bytearray() 250 | # Iterate character by character (Python string iteration handles 251 | # single code point emojis correctly, matching js-multiformats and go-multibase) 252 | for char in emoji_str: 253 | if char not in self.emoji_to_byte: 254 | raise ValueError(f"Non-base256emoji character: {char}") 255 | result.append(self.emoji_to_byte[char]) 256 | return bytes(result) 257 | 258 | 259 | class IdentityConverter: 260 | def encode(self, x): 261 | return x 262 | 263 | def decode(self, x): 264 | return x 265 | -------------------------------------------------------------------------------- /multibase/multibase.py: -------------------------------------------------------------------------------- 1 | from collections import namedtuple 2 | 3 | from morphys import ensure_bytes 4 | 5 | from .converters import ( 6 | Base16StringConverter, 7 | Base32StringConverter, 8 | Base64StringConverter, 9 | Base256EmojiConverter, 10 | BaseStringConverter, 11 | IdentityConverter, 12 | ) 13 | from .exceptions import ( 14 | DecodingError, 15 | InvalidMultibaseStringError, 16 | UnsupportedEncodingError, 17 | ) 18 | 19 | Encoding = namedtuple("Encoding", "encoding,code,converter") 20 | CODE_LENGTH = 1 21 | ENCODINGS = [ 22 | Encoding("identity", b"\x00", IdentityConverter()), 23 | Encoding("base2", b"0", BaseStringConverter("01")), 24 | Encoding("base8", b"7", BaseStringConverter("01234567")), 25 | Encoding("base10", b"9", BaseStringConverter("0123456789")), 26 | Encoding("base16", b"f", Base16StringConverter("0123456789abcdef")), 27 | Encoding("base16upper", b"F", Base16StringConverter("0123456789ABCDEF")), 28 | Encoding("base32hex", b"v", Base32StringConverter("0123456789abcdefghijklmnopqrstuv")), 29 | Encoding("base32hexupper", b"V", Base32StringConverter("0123456789ABCDEFGHIJKLMNOPQRSTUV")), 30 | Encoding("base32hexpad", b"t", Base32StringConverter("0123456789abcdefghijklmnopqrstuv", pad=True)), 31 | Encoding("base32hexpadupper", b"T", Base32StringConverter("0123456789ABCDEFGHIJKLMNOPQRSTUV", pad=True)), 32 | Encoding("base32", b"b", Base32StringConverter("abcdefghijklmnopqrstuvwxyz234567")), 33 | Encoding("base32upper", b"B", Base32StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")), 34 | Encoding("base32pad", b"c", Base32StringConverter("abcdefghijklmnopqrstuvwxyz234567", pad=True)), 35 | Encoding("base32padupper", b"C", Base32StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", pad=True)), 36 | Encoding("base32z", b"h", BaseStringConverter("ybndrfg8ejkmcpqxot1uwisza345h769")), 37 | Encoding("base36", b"k", BaseStringConverter("0123456789abcdefghijklmnopqrstuvwxyz")), 38 | Encoding("base36upper", b"K", BaseStringConverter("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")), 39 | Encoding("base58flickr", b"Z", BaseStringConverter("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ")), 40 | Encoding("base58btc", b"z", BaseStringConverter("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")), 41 | Encoding("base64", b"m", Base64StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")), 42 | Encoding( 43 | "base64pad", 44 | b"M", 45 | Base64StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", pad=True), 46 | ), 47 | Encoding( 48 | "base64url", 49 | b"u", 50 | Base64StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), 51 | ), 52 | Encoding( 53 | "base64urlpad", 54 | b"U", 55 | Base64StringConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", pad=True), 56 | ), 57 | Encoding("base256emoji", "🚀".encode(), Base256EmojiConverter()), 58 | ] 59 | 60 | ENCODINGS_LOOKUP = {} 61 | for codec in ENCODINGS: 62 | ENCODINGS_LOOKUP[codec.encoding] = codec 63 | ENCODINGS_LOOKUP[codec.code] = codec 64 | 65 | 66 | def encode(encoding, data): 67 | """ 68 | Encodes the given data using the encoding that is specified 69 | 70 | :param str encoding: encoding to use, should be one of the supported encoding 71 | :param data: data to encode 72 | :type data: str or bytes 73 | :return: multibase encoded data 74 | :rtype: bytes 75 | :raises UnsupportedEncodingError: if the encoding is not supported 76 | """ 77 | data = ensure_bytes(data, "utf8") 78 | try: 79 | return ENCODINGS_LOOKUP[encoding].code + ENCODINGS_LOOKUP[encoding].converter.encode(data) 80 | except KeyError: 81 | raise UnsupportedEncodingError(f"Encoding {encoding} not supported.") 82 | 83 | 84 | def get_codec(data): 85 | """ 86 | Returns the codec used to encode the given data 87 | 88 | :param data: multibase encoded data 89 | :type data: str or bytes 90 | :return: the :py:obj:`multibase.Encoding` object for the data's codec 91 | :raises InvalidMultibaseStringError: if the codec is not supported 92 | """ 93 | data = ensure_bytes(data, "utf8") 94 | # Check for base256emoji first (4-byte UTF-8 prefix) 95 | if len(data) >= 4: 96 | emoji_prefix = data[:4] 97 | if emoji_prefix in ENCODINGS_LOOKUP: 98 | return ENCODINGS_LOOKUP[emoji_prefix] 99 | 100 | # Check for single-byte prefixes 101 | try: 102 | key = data[:CODE_LENGTH] 103 | codec = ENCODINGS_LOOKUP[key] 104 | except KeyError: 105 | raise InvalidMultibaseStringError(f"Can not determine encoding for {data}") 106 | else: 107 | return codec 108 | 109 | 110 | def is_encoded(data): 111 | """ 112 | Checks if the given data is encoded or not 113 | 114 | :param data: multibase encoded data 115 | :type data: str or bytes 116 | :return: if the data is encoded or not 117 | :rtype: bool 118 | """ 119 | try: 120 | get_codec(data) 121 | return True 122 | except (ValueError, InvalidMultibaseStringError): 123 | return False 124 | 125 | 126 | def is_encoding_supported(encoding): 127 | """ 128 | Check if an encoding is supported. 129 | 130 | :param encoding: encoding name to check 131 | :type encoding: str 132 | :return: True if encoding is supported, False otherwise 133 | :rtype: bool 134 | """ 135 | return encoding in ENCODINGS_LOOKUP 136 | 137 | 138 | def list_encodings(): 139 | """ 140 | List all supported encodings. 141 | 142 | :return: list of encoding names 143 | :rtype: list 144 | """ 145 | return [enc.encoding for enc in ENCODINGS] 146 | 147 | 148 | def get_encoding_info(encoding): 149 | """ 150 | Get information about a specific encoding. 151 | 152 | :param encoding: encoding name 153 | :type encoding: str 154 | :return: Encoding namedtuple with encoding, code, and converter 155 | :rtype: Encoding 156 | :raises UnsupportedEncodingError: if encoding is not supported 157 | """ 158 | if encoding not in ENCODINGS_LOOKUP: 159 | raise UnsupportedEncodingError(f"Encoding {encoding} not supported.") 160 | return ENCODINGS_LOOKUP[encoding] 161 | 162 | 163 | def decode(data, return_encoding=False): 164 | """ 165 | Decode the multibase decoded data 166 | 167 | :param data: multibase encoded data 168 | :type data: str or bytes 169 | :param return_encoding: if True, return tuple (encoding, decoded_data) 170 | :type return_encoding: bool 171 | :return: decoded data, or tuple (encoding, decoded_data) if return_encoding=True 172 | :rtype: bytes or tuple 173 | :raises InvalidMultibaseStringError: if the data is not multibase encoded 174 | :raises DecodingError: if decoding fails 175 | """ 176 | data = ensure_bytes(data, "utf8") 177 | try: 178 | codec = get_codec(data) 179 | # Handle base256emoji which has a 4-byte prefix 180 | prefix_length = len(codec.code) 181 | decoded = codec.converter.decode(data[prefix_length:]) 182 | if return_encoding: 183 | return (codec.encoding, decoded) 184 | return decoded 185 | except (InvalidMultibaseStringError, UnsupportedEncodingError): 186 | # Re-raise these specific exceptions as-is since they already provide 187 | # appropriate context about what went wrong (invalid format or unsupported encoding) 188 | raise 189 | except Exception as e: 190 | # Wrap all other exceptions (e.g., converter errors, invalid data) 191 | # in DecodingError to provide consistent error handling 192 | raise DecodingError(f"Failed to decode multibase data: {e}") from e 193 | 194 | 195 | class Encoder: 196 | """Reusable encoder for a specific encoding.""" 197 | 198 | def __init__(self, encoding): 199 | """ 200 | Initialize an encoder for a specific encoding. 201 | 202 | :param encoding: encoding name to use 203 | :type encoding: str 204 | :raises UnsupportedEncodingError: if encoding is not supported 205 | """ 206 | if encoding not in ENCODINGS_LOOKUP: 207 | raise UnsupportedEncodingError(f"Encoding {encoding} not supported.") 208 | self.encoding = encoding 209 | self._codec = ENCODINGS_LOOKUP[encoding] 210 | 211 | def encode(self, data): 212 | """ 213 | Encode data using this encoder's encoding. 214 | 215 | :param data: data to encode 216 | :type data: str or bytes 217 | :return: multibase encoded data 218 | :rtype: bytes 219 | """ 220 | data = ensure_bytes(data, "utf8") 221 | return self._codec.code + self._codec.converter.encode(data) 222 | 223 | 224 | class Decoder: 225 | """Reusable decoder for multibase data.""" 226 | 227 | def __init__(self): 228 | """Initialize a decoder.""" 229 | pass 230 | 231 | def decode(self, data, return_encoding=False): 232 | """ 233 | Decode multibase encoded data. 234 | 235 | :param data: multibase encoded data 236 | :type data: str or bytes 237 | :param return_encoding: if True, return tuple (encoding, decoded_data) 238 | :type return_encoding: bool 239 | :return: decoded data, or tuple (encoding, decoded_data) if return_encoding=True 240 | :rtype: bytes or tuple 241 | :raises InvalidMultibaseStringError: if the data is not multibase encoded 242 | :raises DecodingError: if decoding fails 243 | """ 244 | return decode(data, return_encoding=return_encoding) 245 | 246 | def or_(self, other_decoder): 247 | """ 248 | Compose this decoder with another, trying this one first. 249 | 250 | This allows trying multiple decoders in sequence. 251 | 252 | :param other_decoder: another decoder to try if this one fails 253 | :type other_decoder: Decoder 254 | :return: a composed decoder 255 | :rtype: ComposedDecoder 256 | """ 257 | return ComposedDecoder([self, other_decoder]) 258 | 259 | 260 | class ComposedDecoder: 261 | """A decoder that tries multiple decoders in sequence.""" 262 | 263 | def __init__(self, decoders): 264 | """ 265 | Initialize a composed decoder. 266 | 267 | :param decoders: list of decoders to try in order 268 | :type decoders: list 269 | """ 270 | self.decoders = decoders 271 | 272 | def decode(self, data, return_encoding=False): 273 | """ 274 | Try to decode with each decoder in sequence. 275 | 276 | :param data: multibase encoded data 277 | :type data: str or bytes 278 | :param return_encoding: if True, return tuple (encoding, decoded_data) 279 | :type return_encoding: bool 280 | :return: decoded data, or tuple (encoding, decoded_data) if return_encoding=True 281 | :rtype: bytes or tuple 282 | :raises DecodingError: if all decoders fail 283 | """ 284 | last_error = None 285 | for decoder in self.decoders: 286 | try: 287 | return decoder.decode(data, return_encoding=return_encoding) 288 | except (InvalidMultibaseStringError, DecodingError) as e: 289 | last_error = e 290 | continue 291 | raise DecodingError(f"All decoders failed. Last error: {last_error}") from last_error 292 | --------------------------------------------------------------------------------