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
- Variables
- type Encoding
- func (enc *Encoding) DecodeString(s string) []byte
- func (enc *Encoding) DecodeStringStrict(s string) ([]byte, error)
- func (enc *Encoding) Direction(forward bool) *Encoding
- func (enc *Encoding) EncodeToString(src []byte) string
- func (enc *Encoding) Shift(shift bool) *Encoding
- func (enc *Encoding) WithLength(withLength bool) *Encoding
Constants ¶
const (
Base62Size = 62
)
Variables ¶
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) )
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 ¶
NewEncoding creates a new Base62 encoder with the given alphabet.
func (*Encoding) DecodeString ¶
DecodeString decodes Base62 string to bytes.
func (*Encoding) DecodeStringStrict ¶
DecodeStringStrict decodes Base62 string to bytes with strict validation.
func (*Encoding) EncodeToString ¶
EncodeToString encodes bytes to Base62 string
func (*Encoding) WithLength ¶ added in v1.2.0
WithLength enables length preservation.