Skip to main content

Library for creating arbitrary binary encodings. Includes variations on base32, base64, base85, and more.

Project description

pybased

Library for creating arbitrary binary encodings. Includes variations on base32, base64, base85, and more.

WARNING: Although these encodings do work end-to-end, they are not compatible with traditional implementations!

I am working on finding out why.

Encodings

Types

  • Sliding: Random-access bitwise implementation, theoretically compatible with bytestrings of arbitrary size.
  • BigInt: Converts bytes into large integers, then incrementally divides by the radix. Incompatible with large files. Suitable for hashes and shorter bytestrings.
  • Biterator: Iterates through each byte from the input stream and adds it to a buffer, then extracts the required number of bits to convert to the other radix. Based roughly off of CPython's base64 encoder, but heavily modified to enable arbitrary conversions. WIP.

Available Encodings

Supported Encodings
IDTypeBits/CharChars/ByteAlphabet
base32Biterator55ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
base32hexBiterator550123456789ABCDEFGHIJKLMNOPQRSTUV
base46BigIntN/AN/AABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz
base62BigIntN/AN/A0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
base64Biterator63ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
base64b64Biterator63./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
base64bashBiterator630123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_
base64bcryptBiterator63./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
base64hqxBiterator63!"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr
base64urlBiterator63ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
base64uuBiterator63 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
base64xxBiterator63+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
base85Biterator77ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+-;<=>?@^_`{|}~
base94BigIntN/AN/A!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
crockford32Biterator550123456789ABCDEFGHJKMNPQRSTVWXYZ
geohash32Biterator550123456789bcdefghjkmnpqrstuvwxyz
nintendo32Biterator550123456789BCDFGHJKLMNPQRSTVWXYZ?
wordsafe32Biterator5523456789CFGHJMPQRVWXcfghjmpqrvwx
zbase32Biterator55ybndrfg8ejkmcpqxot1uwisza345h769

Status

Supported Encodings
StandardEncodedDecodedPassed Test
base32JBSWY3DPFQQHO33SNRSCC===b'Hello, world!'
base32hex91IMOR3F5GG7ERRIDHI22===b'Hello, world!'
base46GnNaagudEKYvFzFrZSSb'Hello, world!'
base621wJfrzvdbthTq5ANZBb'Hello, world!'
base64SGVsbG8sIHdvcmxkIQ==b'Hello, world!'
base64b64G4JgP4wg65RjQalY6E==b'Hello, world!'
base64bashi6lIr6YI87tLsCNA8g==b'Hello, world!'
base64bcryptQETqZE6qGFbtakviGO==b'Hello, world!'
base64hqx5'9XE'mX)(G[FQaN)3==b'Hello, world!'
base64urlSGVsbG8sIHdvcmxkIQ==b'Hello, world!'
base64uu2&5L;&\\L('=O<FQD(0b'Hello, world!'
base64xxG4JgP4wg65RjQalY6E==b'Hello, world!'
base85ERRERR
base94/P\|?l:+>Nq\\sr<+rb'Hello, world!'
crockford3291JPRV3F5GG7EVVJDHJ22b'Hello, world!'
geohash3291kqsv3g5hh7fvvkejk22b'Hello, world!'
nintendo3291LQSW3H5JJ7GWWLFKL22b'Hello, world!'
wordsafe32F3Wgjq5Q7RR9PqqWMVW44b'Hello, world!'
zbase32jb1sa5dxfoo8q551pt1nnb'Hello, world!'
Errors:
base85: c = 99, len = 85, bpc = 7

Getting started

$ pip install pybased

Doing stuff

# Lets's assume we want to use the Crockford32 encoding scheme.
from based.standards.base32 import crockford32

# And let's assume the variable data has what we want to encode.
data: bytes = ...

# Encode to string.
encoded: str = crockford32.encode_bytes(data)

# ...

# Decode the string back to bytes.
data: bytes = crockford32.decode_bytes(encoded)

based Command-Line Tool

$ based --help
usage: based [-h] {dump,encode,decode} ...

positional arguments:
  {dump,encode,decode}

optional arguments:
  -h, --help            show this help message and exit

NOTE: The based CLI tool is currently only useful for testing, and is under very active development.

Encode string to Base94

$ based encode --standard=base94 --input-string 'Hello, world!'
/P|?l:+>Nq\sr<+r

Encode string to Base94 and output JSON

$ based encode --standard=base94 --input-string 'Hello, world!' --output-format json
{"encoded": "/P|?l:+>Nq\\sr<+r"}

Decode data from Base94

$ based decode --standard=base94 --input-string '/P|?l:+>Nq\sr<+r'
>>> input chars: 16B
Bytes representation: b'Hello, world!'
Hex representation: 48656c6c6f2c20776f726c6421

Various Output Encoding Formats

$ based decode --standard=base94 --input-string '/P|?l:+>Nq\sr<+r' --output-format json
>>> input chars: 16B
{"b64": "SGVsbG8sIHdvcmxkIQ==", "utf-8": "Hello, world!"}
$ based decode --standard=base94 --input-string '/P|?l:+>Nq\sr<+r' --output-format yaml
>>> input chars: 16B
encoded: !!binary |
  SGVsbG8sIHdvcmxkIQ==

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

pybased-0.0.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

pybased-0.0.2-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file pybased-0.0.2.tar.gz.

File metadata

  • Download URL: pybased-0.0.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pybased-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8ba07a073cec9f33207f2e4b99279b864d284b20f6bf18df933d84b2d520d0cf
MD5 14d80b10429e4df889ab0399c0117d10
BLAKE2b-256 c977dd461e9d50531d6f9f4ea3f6ff5a2acf7d38eeafb59071422fb5a8a324d1

See more details on using hashes here.

File details

Details for the file pybased-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pybased-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pybased-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dca1f212c41e5720c56c858575f502b39ea132810ef12f5c2c354edd6faae34b
MD5 954f2f63f77510e2803fcaccc16bf7b5
BLAKE2b-256 b32752fb7875822bac8ff9396bb95f69d152f512466c4d68cca635eb740a31b8

See more details on using hashes here.

Supported by

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