Skip to main content

A flexible base-N encoder/decoder implementation supporting standard and custom alphabets

Project description

Base-Flex Encoder/Decoder

A flexible Python implementation for encoding and decoding data using various base-N encodings (e.g., Base64, Base32, Base16, Base4096, etc.). The implementation supports custom alphabets and optional separators between encoded characters.

Note: If you are looking for a faster, more robust implementation for standart encodings, use the standard library. This library goal is to provide a flexible implementation for custom base-N encodings and alphabets!

Features

  • Support for any base-N encoding with power-of-2 alphabet size
  • Built-in support for common encodings (Base64, Base32)
  • Pre-defined standard alphabets in alphabets module
  • Configurable padding character
  • Optional separators between encoded characters
  • Comprehensive test suite
  • Type hints and detailed documentation
  • Efficient implementation using bitwise operations

Installation

pip install base-flex

Usage

from base_flex import BaseN
from base_flex.alphabets import BASE64_ALPHABET, BASE32_ALPHABET

# Base64 encoding/decoding
base64 = BaseN(list(BASE64_ALPHABET))

# Basic encoding/decoding
encoded = base64.encode(b"Hello, World!")
decoded = base64.decode(encoded)

# Using separators for better readability
base64_sep = BaseN(list(BASE64_ALPHABET), separator="-")
encoded_with_sep = base64_sep.encode(b"Hello, World!")
decoded_with_sep = base64_sep.decode(encoded_with_sep)

# Base32 encoding/decoding
base32 = BaseN(list(BASE32_ALPHABET))
encoded_base32 = base32.encode(b"Hello, World!")
decoded_base32 = base32.decode(encoded_base32)

# Custom base-N encoding
# You can create your own base-N encoding by providing a custom alphabet
# The alphabet length (excluding padding char) must be a power of 2
# The last character of the alphabet is the padding character, and every element must be unique!
custom_alphabet = list("01234567=")  # Base8 example
base8 = BaseN(custom_alphabet)
encoded_base8 = base8.encode(b"Hello")
decoded_base8 = base8.decode(encoded_base8)

Project Structure

base_flex/
├── base_flex/
│   ├── __init__.py
│   ├── base_flex.py      # Main implementation
│   ├── alphabets.py   # Pre-defined standard alphabets
│   └── tests/
│       ├── __init__.py
│       └── test_base_flex.py
├── README.md
└── pyproject.toml

Running Tests

The project includes a comprehensive test suite. To run the tests:

python -m unittest discover -v && coverage report

Implementation Details

The implementation follows these steps:

Encoding

  1. Converts input bytes to a binary string
  2. Splits the binary string into n-bit chunks
  3. Converts each chunk to a base-N character
  4. Adds padding if necessary
  5. Joins characters with separator (if specified)

Decoding

  1. Removes separators and padding
  2. Converts each character back to its binary representation
  3. Concatenates binary strings
  4. Converts binary string back to bytes

Validation

  • Ensures alphabet length (excluding padding) is a power of 2
  • Checks for duplicate characters in the alphabet
  • Validates input characters during decoding

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

base_flex-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

base_flex-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file base_flex-0.1.0.tar.gz.

File metadata

  • Download URL: base_flex-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.8 Darwin/24.2.0

File hashes

Hashes for base_flex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4cd488dcb284c493f7ce1972f0b4b4040a1abfaaa17646f06d93f505e1c7fe6
MD5 b3250d5a858c109d61a254df6ce2ec89
BLAKE2b-256 f6139850e6106f60f14e28905d6640749feb18105eb0f7e8452a05b2d0df4881

See more details on using hashes here.

File details

Details for the file base_flex-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: base_flex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.8 Darwin/24.2.0

File hashes

Hashes for base_flex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e447d0cc95edc6c9e06e2eb67727e0fe4a88807634ab3d6a2e1421342fcd451
MD5 d378bd7da865c9a83cc3a65791e7afb5
BLAKE2b-256 5b3beb13eb14d92efa46222bb678970319ce57f7a600752b46e6aad7f44017fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page