basex

package module
v0.0.0-...-731697a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2018 License: MIT Imports: 2 Imported by: 1

README

base-x

GoDoc codecov Build Status

Fast base encoding / decoding of any given alphabet using bitcoin style leading zero compression.

Example

Base58


import (
	"fmt"
	"testing"

	"github.com/dignifiedquire/go-basex"
)

func TestExample(t *testing.T) {
	Base58Charset := "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
	bs58 := basex.NewAlphabet(Base58Charset)

	decoded, err := bs58.Decode("5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%x\n", decoded)
	// => 80eddbdc1168f1daeadbd3e44c1e3f8f5a284c2029f78ad26af98583a499de5b1913a4f863

	fmt.Printf("%s\n", 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
36 0123456789abcdefghijklmnopqrstuvwxyz
58 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
62 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
64 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
66 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. If you need standard hex encoding, or base64 encoding, this module is NOT appropriate.

Credit

This is a reimplementation of the great package cryptcoinjs/base-x.

LICENSE

MIT

Documentation

Overview

Package basex is a port of [base-x](https://github.com/cryptocoinjs/base-x) to Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alphabet

type Alphabet struct {
	// contains filtered or unexported fields
}

Alphabet holds an arbitrary base alphabet.

func NewAlphabet

func NewAlphabet(raw string) *Alphabet

NewAlphabet creates a new alphabet by generating the necessariy lookup tables.

func (*Alphabet) Base

func (a *Alphabet) Base() uint

Base returns the base size of this alphabet.

func (*Alphabet) Decode

func (a *Alphabet) Decode(input string) ([]byte, error)

Decode decodes the given string into the original bytes.

func (*Alphabet) DecodeFromBytes

func (a *Alphabet) DecodeFromBytes(input []byte) ([]byte, error)

DecodeFromBytes decodes a byte slice in the range of 0 to base, into the original data.

func (*Alphabet) Encode

func (a *Alphabet) Encode(input []byte) string

Encode encodes the given input using the current alphabet.

func (*Alphabet) EncodeToBytes

func (a *Alphabet) EncodeToBytes(input []byte) []byte

EncodeToBytes encodes the given input only using the base and returns a byte slice with entries in the range from 0 to base.

func (*Alphabet) String

func (a *Alphabet) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL