Skip to main content

Encoding/decoding of any given alphabet with any base using bitcoin style

Project description

basex

Encoding/decoding of any given alphabet with any base using bitcoin style leading zero compression

This is a port of js base-x package from cryptocoinjs/base-x.

WARNING: This module is NOT RFC3548 compliant, it cannot be used for base16 (hex), base32, or base64 encoding in a standards compliant manner.

Example

Base58

from basex import basex

BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
bs58 = basex(BASE58)

decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')

print(decoded)
# => bytearray(b'\x80\xed\xdb\xdc\x11h\xf1\xda\xea\xdb\xd3\xe4L\x1e?\x8fZ(L )\xf7\x8a\xd2j\xf9\x85\x83\xa4\x99\xde[\x19\x13\xa4\xf8c')

print(bs58.encode(decoded))
# => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr

Alphabets

See below for a list of commonly recognized alphabets, and their respective base.

Base Alphabet
2 01
8 01234567
11 0123456789a
16 0123456789abcdef
32 0123456789ABCDEFGHJKMNPQRSTVWXYZ
32 ybndrfg8ejkmcpqxot1uwisza345h769 (z-base-32)
36 0123456789abcdefghijklmnopqrstuvwxyz
58 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
62 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
64 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
67 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~

How it works

It encodes octet arrays by doing long divisions on all significant digits in the array, creating a representation of that number in the new base. Then for every leading zero in the input (not significant as a number) it will encode as a single leader character. This is the first in the alphabet and will decode as 8 bits. The other characters depend upon the base. For example, a base58 alphabet packs roughly 5.858 bits per character.

This means the encoded string 000f (using a base16, 0-f alphabet) will actually decode to 4 bytes unlike a canonical hex encoding which uniformly packs 4 bits into each character.

While unusual, this does mean that no padding is required and it works for bases like 43.

LICENSE MIT

A direct derivation of the base58 implementation from bitcoin/bitcoin, generalized for variable length alphabets.

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_x-1.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

base_x-1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file base_x-1.2.tar.gz.

File metadata

  • Download URL: base_x-1.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for base_x-1.2.tar.gz
Algorithm Hash digest
SHA256 6f0d5636c081e8bbc46b3be5b999d9a3908c0f7f69c6335e31b570c5d6073e21
MD5 62bb984b16cd56f9bf53eb06c8536a46
BLAKE2b-256 d428de39836ce97ba1c786b176460f002cd72e2e55d524498589e8dec9d31b6e

See more details on using hashes here.

File details

Details for the file base_x-1.2-py3-none-any.whl.

File metadata

  • Download URL: base_x-1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for base_x-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5787047f5ecaf0df04ad72c683fcb76b334dba6f6988790ab791fb515e39d998
MD5 8eb8c1df3f38bc66afdb3fd815ac7b82
BLAKE2b-256 a5194f99bb500620bc04a43b6ed06f900d9c0fd9369756958c8a153130e829d7

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