Fast base encoding/decoding of any given alphabet
npm install @herbcaudill/base-xFast base encoding / decoding of any given alphabet using bitcoin style leading zero compression.
WARNING: This module is NOT RFC3548 compliant, it cannot be used for base16 (hex), base32, or base64 encoding in a standards compliant manner.
Base58
``javascript
import { basex } from 'base-x'
var BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var bs58 = basex(BASE58)
var decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')
console.log(decoded)
// =>
console.log(bs58.encode(decoded))
// => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr
`
See below for a list of commonly recognized alphabets, and their respective base.
| base | alphabet | export |
| ---- | --------------------------------------------------------------------- | --------- |
| 2 | 01 | base2 |01234567
| 8 | | base8 |0123456789a
| 11 | | base11 |0123456789abcdef
| 16 | | base16 |0123456789ABCDEFGHJKMNPQRSTVWXYZ
| 32 | | base32 |ybndrfg8ejkmcpqxot1uwisza345h769
| 32 | (z-base-32) | basez32 |0123456789abcdefghijklmnopqrstuvwxyz
| 36 | | base36 |123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
| 58 | | base58 |0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
| 62 | | base62 |ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
| 64 | | base64 |ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~
| 67 | | base67 |
For convenience, these are exported with the names shown above. For example, the above code could be written as follows:
`javascript
import { bs58 } from 'base-x'
var decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')
console.log(decoded)
// =>
console.log(bs58.encode(decoded))
// => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr
``
Adapted from https://github.com/cryptocoinjs/base-x