base56

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2022 License: MIT Imports: 3 Imported by: 1

README

base56 MIT License GitHub Release GoDoc Go Report Card

import "toolman.org/encoding/base56"

Install

  go get toolman.org/encoding/base56

Overview

Package base56 provides functions for encoding/decoding uint64 values as short, easily digestible, base56 strings. For example, the Std encoding transforms the uint64 value 1540840132 into the string "2nfvKM".

For compatibility with other implementions, this package supports three separate and distinct base56 character sets through the package level, Encoding variables Std, Alt and Py3. Each of these Encodings leverage only 7-bit clean (ASCII) characters.

The Std Encoding employs the original character set used by this package and is composed of the numerals 0-9 followed by all upper case characters except for 'D' (0x44), 'I' (0x49), 'O' (0x4f), and 'Q' (0x51) and then all lower case characters except for 'i' (0x69) and 'o' (0x6f).

The Alt Encoding is compatible with PHP and Java implementations and is defined as the numerals 2-9 followed by all lower case characters except for 'l' (0x6c) and 'o' (0x6f) and then all upper case characters except for 'I' (0x49) and 'O' (0x4f).

The Py3 Encoding is compatible with the Python-3 implementation and is defined as the numerals 2-9 followed by all upper case characters except for 'I' (0x49) and 'O' (0x4f) and then all lower case characters except for 'l' (0x6c) and 'o' (0x6f).

Note, the Alt and Py3 Encodings are identical except for the order of character classes. Alt is numerals->lowercase->uppercase while Py3 is numerals->uppercase->lowercase.

Documentation

Overview

Package base56 provides functions for encoding/decoding uint64 values as short, easily digestible, base56 strings.

For compatibility with other implementions, this package supports three separate and distinct base56 character sets through the package level, Encoding variables Std, Alt and Py3. Each of these Encodings leverage only 7-bit clean (ASCII) characters.

The Std Encoding employs the original character set used by this package and is composed of the numerals 0-9 followed by all upper case characters except for 'D' (0x44), 'I' (0x49), 'O' (0x4f), and 'Q' (0x51) and then all lower case characters except for 'i' (0x69) and 'o' (0x6f).

The Alt Encoding is compatible with PHP and Java implementations and is defined as the numerals 2-9 followed by all lower case characters except for 'l' (0x6c) and 'o' (0x6f) and then all upper case characters except for 'I' (0x49) and 'O' (0x4f).

The Py3 Encoding is compatible with the Python-3 implementation and is defined as the numerals 2-9 followed by all upper case characters except for 'I' (0x49) and 'O' (0x4f) and then all lower case characters except for 'l' (0x6c) and 'o' (0x6f).

Note, the Alt and Py3 Encodings are identical except for the order of character classes. Alt is numerals->lowercase->uppercase while Py3 is numerals->uppercase->lowercase.

For reference, here are links to the known implementations for other languages:

PHP....: http://rossduggan.ie/blog/codetry/base-56-integer-encoding-in-php/index.html
Java...: ??
Python.: https://github.com/jyn514/base56

Index

Constants

This section is empty.

Variables

View Source
var (
	// Std is the standard character set traditionally used by this package.
	Std = charSet("0123456789ABCEFGHJKLMNPRSTUVWXYZabcdefghjklmnpqrstuvwxyz")

	// Alt is an alternative character set used by PHP and Java implementations.
	Alt = charSet("23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ")

	// Py3 is the character set used by the Python base56 library.
	Py3 = charSet("23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz")
)
View Source
var ErrNotBase56 = errors.New("invalid base56 value")

ErrNotBase56 is returned by Decode if it is provided an invalid base56 value for the associated Encoding.

Functions

func Decode

func Decode(s string) (uint64, error)

Decode is a convenience wrapper around Std.Decode.

func Encode

func Encode(i uint64) string

Encode is a convenience wrapper around Std.Encode.

func Hashf

func Hashf(format string, args ...interface{}) string

Hashf is a convenience wrapper around Std.Hashf.

Types

type Encoding added in v1.2.0

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

Encoding represents a distinct base56 character set.

func (*Encoding) Decode added in v1.2.0

func (e *Encoding) Decode(s string) (uint64, error)

Decode accepts a valid, base56 string -- as returned by Encode -- and returns its uint64 value, or zero and ErrNotBase56 if the base56 string is invalid. Valid base56 values are composed of characters from the receiver's defined character set.

func (*Encoding) Encode added in v1.2.0

func (e *Encoding) Encode(i uint64) string

Encode accepts a uint64 value and encodes it to a base56 string composed of characters from the receiver's defined character set.

func (*Encoding) Hashf added in v1.2.0

func (e *Encoding) Hashf(format string, args ...interface{}) string

Hashf feeds its printf-like arguments into an FNV-1 hash (see "hash/fnv"), then returns the hashed 64 bit sum as a base56 encoded string according to rhe receiver's encoding character set.

Jump to

Keyboard shortcuts

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