base58

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: Unlicense, Unlicense Imports: 2 Imported by: 3

README

Base58

This is a Go language package implementing base58 encoding/decoding algorithms. This implementation attempts to optimize performance of encoding and decoding data to be used with other applications.

There are a few existing implementations and this package takes an inspiration (and hints on optimizations) from a few of them (see Acknowledgements for details).

Why base58?

There are other widely used methods to encode/decode raw data into printable format. Most common onces are HEX and base64. While those are good approaches in some situations, each of them has own limitations. HEX tends to be long and base64 is hard to understand and read, they still have a place to be used when storage and readability are not of concern. Base58 encoding serves double purpose - 1st it allows the long data to be presented in short format (compression of sorts) and 2nd it produces human readable output by removing unnecessary and ambigous characters.

The alphabet of this encoding can be presented as below:

Alphabet:       123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
Base58:         123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

Installation

To install and use this package simply use standard go tools:

$ go get -x -u github.com/dyammarcano/base58

And then include this package in your project

import "github.com/dyammarcano/base58"

Usage

base58.Encode takes slice of bytes. The reason for this is to be able to encode ANY types of data (as long as it is possible to present it as a slice of bytes):

    var unencoded []byte = ...
    var encoded string = base58.StdEncoding.EncodeToString(unencoded)

base58.Decode takes string as an input and returns slice of bytes, which can be converted/cast to original type. If the passed string contains characters invalid for base58 encoding then the returned error is not nil:

    var encoded string = ...
    decoded, err := base58.StdEncoding.Decode(encoded)
    if err != nil {
        ...
    }

Example

Below is example of encoding and decoding random bytes:

package main

import (
    "crypto/rand"
    "log"
    "fmt"
    "encoding/hex"
    "github.com/dyammarcano/base58"
)

func main() {
    // Generate random data to be encoded
    data := make([]byte, 64)
    _, err := rand.Read(data)
    if err != nil {
        log.Fatal(err)
    }
    
    // Print generated data in HEX format
    fmt.Println("Random data to encode: ", hex.EncodeToString(data))
    
    // Encode generated data to base58 and print it
    encoded := base58.StdEncoding.Encode(data)
    fmt.Println("Base58 encoded data  : ", encoded)
    
    // Decode base58 string to bytes and print it in HEX format for inspection
    decoded, err := base58.StdEncoding.Decode(encoded)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Decoded base58 data  : ", hex.EncodeToString(decoded))
}

Running the tests

To run tests and benchmarks simply use:

$ cd $(go env GOPATH)/src/github.com/dyammarcano/base58
$ go test
$ go test -bench=.

Contributing

I welcome any contribution to this library, especially those targeting performance improvements.

Please read CONTRIBUTING beforehand.

License

This project is licensed under the UNLICENSE License - see the LICENSE file for details

Acknowledgments

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StdEncoding = NewEncoding()

StdEncoding is the standard base58 encoding, as defined in RFC 2119.

Functions

This section is empty.

Types

type Encoding

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

func NewEncoding

func NewEncoding() *Encoding

func (*Encoding) Decode

func (enc *Encoding) Decode(input string) (output []byte, err error)

Decode converts a base58 string to a slice of bytes, and returns an error if the input is not valid base58 string.

func (*Encoding) DecodeString

func (enc *Encoding) DecodeString(s string) ([]byte, error)

func (*Encoding) Encode

func (enc *Encoding) Encode(input []byte) string

Encode converts a slice of bytes to a base58 string.

func (*Encoding) EncodeToString

func (enc *Encoding) EncodeToString(src []byte) string

Jump to

Keyboard shortcuts

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