base62

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 5 Imported by: 5

README

goth-base62

Documentation

Overview

Package base62 provides efficient Base62 encoding with multiple encoding variants.

Base62 encoding uses 62 characters (0-9, A-Z, a-z) for URL-safe, readable output. Six encoding variants are supported:

  • StdEncoding: Standard encoding with length preservation (default)
  • FlipEncoding: Reversed character order with length preservation (default)
  • ShiftEncoding: Random shift for additional entropy
  • FlipShiftEncoding: Combined reverse and shift
  • StdLengthEncoding: Alias for StdEncoding (deprecated, use StdEncoding)
  • FlipLengthEncoding: Alias for FlipEncoding (deprecated, use FlipEncoding)

Important behaviors:

  • StdEncoding and FlipEncoding preserve original length including leading zeros
  • ShiftEncoding and FlipShiftEncoding preserve original length with random encoding
  • All encoders are goroutine-safe

For AES encryption/decryption or any use case requiring exact byte length preservation, use StdEncoding or FlipEncoding (both preserve length by default).

Index

Constants

View Source
const (
	Base62Size = 62
)

Variables

View Source
var (

	// StdEncoding is the standard Base62 encoder with length preservation
	StdEncoding = NewEncoding(encodeStd).WithLength(true)

	// FlipEncoding reverses character order with length preservation
	FlipEncoding = NewEncoding(encodeStd).Direction(true).WithLength(true)

	// ShiftEncoding adds random shift for entropy
	ShiftEncoding = NewEncoding(encodeStd).Shift(true)

	// FlipShiftEncoding combines reverse and shift
	FlipShiftEncoding = NewEncoding(encodeStd).Shift(true).Direction(true)

	StdLengthEncoding  = NewEncoding(encodeStd).WithLength(true)
	FlipLengthEncoding = NewEncoding(encodeStd).Direction(true).WithLength(true)
)
View Source
var ErrInvalidCharacter = errors.New("base62: invalid character")

ErrInvalidCharacter indicates an invalid character during decoding

Functions

This section is empty.

Types

type Encoding

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

func NewEncoding

func NewEncoding(encoder string) *Encoding

NewEncoding creates a new Base62 encoder with the given alphabet.

func (*Encoding) DecodeString

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

DecodeString decodes Base62 string to bytes.

func (*Encoding) DecodeStringStrict

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

DecodeStringStrict decodes Base62 string to bytes with strict validation.

func (*Encoding) Direction

func (enc *Encoding) Direction(forward bool) *Encoding

Direction sets encoding direction.

func (*Encoding) EncodeToString

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

EncodeToString encodes bytes to Base62 string

func (*Encoding) Shift

func (enc *Encoding) Shift(shift bool) *Encoding

Shift enables shift encoding.

func (*Encoding) WithLength added in v1.2.0

func (enc *Encoding) WithLength(withLength bool) *Encoding

WithLength enables length preservation.

Jump to

Keyboard shortcuts

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