Splurge Base58 Helper
Project description
splurge-base58
A Python library for Base-58 encoding and decoding operations with both CLI and API interfaces.
Features
- Pure Python Implementation: No external dependencies required
- Bitcoin-Compatible Alphabet: Uses the standard Bitcoin Base-58 alphabet
- CLI Interface: Command-line tool for quick encoding/decoding operations
- API Interface: Python class for programmatic use
- Input Validation: Comprehensive validation with meaningful error messages
- Length Constraints: Configurable input length limits for security
- Unicode Support: Full support for UTF-8 encoded strings
- Error Handling: Robust error handling with custom exception types
- Performance Optimized: Efficient algorithms for encoding and decoding
Installation
pip install splurge-base58
CLI Usage
The CLI provides a simple command-line interface for Base-58 operations.
Basic Commands
# Encode a string to Base-58
python -m splurge_base58 encode "Hello, World!"
# Decode a Base-58 string
python -m splurge_base58 decode "JxF12TrwUP45BMd"
Command Syntax
python -m splurge_base58 <command> <input>
Commands:
encode- Convert input string to Base-58 encodingdecode- Convert Base-58 string back to original data
Constraints:
encode: Maximum input length is 2048 charactersdecode: Maximum input length is calculated based on maximum encode length
Examples
# Encode various types of data
python -m splurge_base58 encode "Hello, World!"
python -m splurge_base58 encode "1234567890"
python -m splurge_base58 encode "Special chars: !@#$%^&*()"
# Decode Base-58 strings
python -m splurge_base58 decode "JxF12TrwUP45BMd"
python -m splurge_base58 decode "2NEpo7TZRRrLZSi2U"
python -m splurge_base58 decode "11111111111111111111111111111111"
# Error handling examples
python -m splurge_base58 decode "invalid!@#" # Invalid Base-58
python -m splurge_base58 encode "" # Empty input
Error Handling
The CLI provides clear error messages for various scenarios:
- Invalid Base-58 string: When decode input contains invalid characters
- Empty input: When encode input is empty
- Input too long: When input exceeds maximum length constraints
- Unknown command: When an invalid command is provided
API Usage
The Base58 class provides a comprehensive API for Base-58 operations.
Basic Usage
from splurge_base58.base58 import Base58, Base58Error
# Encode data
data = "Hello, World!".encode('utf-8')
encoded = Base58.encode(data)
print(encoded) # Output: JxF12TrwUP45BMd
# Decode data
decoded = Base58.decode(encoded)
original = decoded.decode('utf-8')
print(original) # Output: Hello, World!
Class Methods
Base58.encode(data: bytes) -> str
Encodes binary data to a Base-58 string.
# Encode string data
text = "Hello, World!"
data = text.encode('utf-8')
encoded = Base58.encode(data)
# Encode binary data
binary_data = b'\x00\x01\x02\x03'
encoded = Base58.encode(binary_data)
# Encode hash data
import hashlib
hash_data = hashlib.sha256(b"test").digest()
encoded = Base58.encode(hash_data)
Base58.decode(base58_data: str) -> bytes
Decodes a Base-58 string back to binary data.
# Decode to string
encoded = "JxF12TrwUP45BMd"
decoded = Base58.decode(encoded)
text = decoded.decode('utf-8')
# Decode binary data
encoded = "11111111111111111111111111111111"
decoded = Base58.decode(encoded)
print(decoded.hex()) # Output: 00000000000000000000000000000000
Base58.is_valid(base58_data: str) -> bool
Validates if a string is valid Base-58.
# Valid Base-58 strings
Base58.is_valid("JxF12TrwUP45BMd") # True
Base58.is_valid("11111111111111111111111111111111") # True
# Invalid Base-58 strings
Base58.is_valid("invalid!@#") # False
Base58.is_valid("") # False
Error Handling
The API uses custom exception types for different error scenarios:
from splurge_base58.base58 import Base58, Base58Error, Base58TypeError, Base58ValidationError
try:
# Encode with invalid input type
Base58.encode("not bytes") # Raises Base58TypeError
# Encode empty data
Base58.encode(b"") # Raises Base58ValidationError
# Decode invalid Base-58
Base58.decode("invalid!@#") # Raises Base58ValidationError
except Base58TypeError as e:
print(f"Type error: {e}")
except Base58ValidationError as e:
print(f"Validation error: {e}")
except Base58Error as e:
print(f"Base-58 error: {e}")
Practical Examples
Encoding JSON Data
import json
from splurge_base58.base58 import Base58
# Encode JSON payload
data = {
"user_id": 12345,
"timestamp": "2024-01-15T10:30:00Z",
"action": "login"
}
json_string = json.dumps(data)
json_bytes = json_string.encode('utf-8')
encoded = Base58.encode(json_bytes)
# Decode and verify
decoded = Base58.decode(encoded)
decoded_json = decoded.decode('utf-8')
restored_data = json.loads(decoded_json)
Encoding File Data
from splurge_base58.base58 import Base58
# Encode file header (example: PNG header)
file_header = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01'
encoded = Base58.encode(file_header)
# Decode file data
decoded = Base58.decode(encoded)
print(decoded.hex()) # Output: 89504e470d0a1a0a0000000d494844520000000100000001
Encoding Hash Values
import hashlib
from splurge_base58.base58 import Base58
# Encode SHA-256 hash
password = "password123"
hash_data = hashlib.sha256(password.encode('utf-8')).digest()
encoded = Base58.encode(hash_data)
# Decode hash
decoded = Base58.decode(encoded)
print(decoded.hex()) # Output: 240be01fab5649d2beb87e2d6a5574d
Performance Considerations
The implementation is optimized for performance:
- Efficient algorithms: Uses optimized conversion methods
- Memory efficient: Minimal memory overhead during operations
- Fast validation: Quick validation of Base-58 strings
For large datasets, consider processing data in chunks if needed.
Examples
See the examples/ directory for complete working examples:
cli_usage.py- CLI end-to-end workflow examplesapi_usage.py- API end-to-end workflow examples
Run the examples:
# CLI examples
python examples/cli_usage.py
# API examples
python examples/api_usage.py
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
[2025.1.1] - 2025-01-15
- Added comprehensive changelog section to README.md
- Improved test suite organization and reliability
- Enhanced single character decoding test logic with round-trip verification
- Reorganized imports in test files for better code structure
- Improved exception handling test coverage
[2025.1.0] - 2025-08-16
- Initial Commit
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file splurge_base58-2025.1.1.tar.gz.
File metadata
- Download URL: splurge_base58-2025.1.1.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d45c3970c07f9f521afc36311181c55cb582f37877215ff3ed02fa0099db7cc0
|
|
| MD5 |
ef1b10912efba08cb8068b0faf90cc14
|
|
| BLAKE2b-256 |
ce80449ab00b9338a8f70f94bc00cbe4e58ef2b6cf33cef4258603676f201d2c
|
File details
Details for the file splurge_base58-2025.1.1-py3-none-any.whl.
File metadata
- Download URL: splurge_base58-2025.1.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2a6b5228f50560aea33f2a973bee4c961361540c3408e373eff634922fd040e
|
|
| MD5 |
45b32b784161b884f9238bf52513501c
|
|
| BLAKE2b-256 |
95f55ec30d2bc6f32eec86dcbce3a13019e4a1afee53232d13273ab97b8bbb67
|