base58

package
v0.0.0-...-680e691 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2021 License: Apache-2.0, MIT Imports: 1 Imported by: 0

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

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.

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.

Jump to

Keyboard shortcuts

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