base58

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: MIT Imports: 2 Imported by: 933

README

Fast Implementation of Base58 encoding

GoDoc Go Report Card Used By

Fast implementation of base58 encoding in Go.

Base algorithm is copied from https://github.com/trezor/trezor-crypto/blob/master/base58.c

Benchmark

Trivial - encoding via big.Int (over libraries use this implemenation) Fast - optimized algorythm from trezor

BenchmarkTrivialBase58Encoding-4   	  200000	     10602 ns/op  
BenchmarkFastBase58Encoding-4      	 1000000	      1637 ns/op
BenchmarkTrivialBase58Decoding-4   	  200000	      8316 ns/op
BenchmarkFastBase58Decoding-4      	 1000000	      1045 ns/op

Encoding - faster by 6 times

Decoding - faster by 8 times

Usage example


package main

import (
	"fmt"
	"github.com/mr-tron/base58"
)

func main() {

	encoded := "1QCaxc8hutpdZ62iKZsn1TCG3nh7uPZojq"
	num, err := base58.Decode(encoded)
	if err != nil {
		fmt.Printf("Demo %v, got error %s\n", encoded, err)	
	}
	chk := base58.Encode(num)
	if encoded == string(chk) {
		fmt.Printf ( "Successfully decoded then re-encoded %s\n", encoded )
	} 
}

Documentation

Overview

Package base58 provides fast implementation of base58 encoding.

Base58 Usage

To decode a base58 string:

encoded := "1QCaxc8hutpdZ62iKZsn1TCG3nh7uPZojq"
buf, _ := base58.Decode(encoded)

To encode the same data:

encoded := base58.Encode(buf)

With custom alphabet

customAlphabet := base58.NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
encoded := base58.EncodeAlphabet(buf, customAlphabet)

Index

Constants

This section is empty.

Variables

View Source
var BTCAlphabet = NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

BTCAlphabet is the bitcoin base58 alphabet.

View Source
var FlickrAlphabet = NewAlphabet("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ")

FlickrAlphabet is the flickr base58 alphabet.

Functions

func Decode

func Decode(str string) ([]byte, error)

Decode decodes the base58 encoded bytes.

func DecodeAlphabet

func DecodeAlphabet(str string, alphabet *Alphabet) ([]byte, error)

DecodeAlphabet decodes the base58 encoded bytes using the given b58 alphabet.

func Encode

func Encode(bin []byte) string

Encode encodes the passed bytes into a base58 encoded string.

func EncodeAlphabet

func EncodeAlphabet(bin []byte, alphabet *Alphabet) string

EncodeAlphabet encodes the passed bytes into a base58 encoded string with the passed alphabet.

func FastBase58Decoding

func FastBase58Decoding(str string) ([]byte, error)

FastBase58Decoding decodes the base58 encoded bytes.

func FastBase58DecodingAlphabet

func FastBase58DecodingAlphabet(str string, alphabet *Alphabet) ([]byte, error)

FastBase58DecodingAlphabet decodes the base58 encoded bytes using the given b58 alphabet.

func FastBase58Encoding

func FastBase58Encoding(bin []byte) string

FastBase58Encoding encodes the passed bytes into a base58 encoded string.

func FastBase58EncodingAlphabet

func FastBase58EncodingAlphabet(bin []byte, alphabet *Alphabet) string

FastBase58EncodingAlphabet encodes the passed bytes into a base58 encoded string with the passed alphabet.

func TrivialBase58Decoding

func TrivialBase58Decoding(str string) ([]byte, error)

TrivialBase58Decoding decodes the base58 encoded bytes (inefficiently).

func TrivialBase58DecodingAlphabet

func TrivialBase58DecodingAlphabet(str string, alphabet *Alphabet) ([]byte, error)

TrivialBase58DecodingAlphabet decodes the base58 encoded bytes (inefficiently) using the given b58 alphabet.

func TrivialBase58Encoding

func TrivialBase58Encoding(a []byte) string

TrivialBase58Encoding encodes the passed bytes into a base58 encoded string (inefficiently).

func TrivialBase58EncodingAlphabet

func TrivialBase58EncodingAlphabet(a []byte, alphabet *Alphabet) string

TrivialBase58EncodingAlphabet encodes the passed bytes into a base58 encoded string (inefficiently) with the passed alphabet.

Types

type Alphabet

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

Alphabet is a a b58 alphabet.

func NewAlphabet

func NewAlphabet(s string) *Alphabet

NewAlphabet creates a new alphabet from the passed string.

It panics if the passed string is not 58 bytes long or isn't valid ASCII.

Directories

Path Synopsis
examples
cli
wif

Jump to

Keyboard shortcuts

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