base64

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package base64 implements base64 encoding and decoding with streaming support. It provides both standard and URL-safe base64 alphabets, along with streaming capabilities for efficient processing of large data. Base64 encoding follows RFC 4648 standard for binary-to-text encoding.

Index

Constants

This section is empty.

Variables

View Source
var StdAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

StdAlphabet is the standard base64 alphabet as defined in RFC 4648. It uses uppercase letters A-Z, lowercase letters a-z, digits 0-9, plus sign (+), and forward slash (/) for a total of 64 characters. This is the most commonly used base64 alphabet for general purpose encoding.

View Source
var URLAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

URLAlphabet is the URL-safe base64 alphabet as defined in RFC 4648. It uses uppercase letters A-Z, lowercase letters a-z, digits 0-9, minus sign (-), and underscore (_) for a total of 64 characters. This alphabet is safe for use in URLs and filenames as it avoids characters that have special meaning in these contexts.

Functions

func Decode added in v1.1.1

func Decode(src []byte) []byte

Decode decodes the given base64-encoded byte slice using standard base64 decoding. This is a convenience function that creates a new decoder and decodes the input. Returns the decoded data, ignoring any decoding errors.

func DecodeURLSafe added in v1.1.1

func DecodeURLSafe(src []byte) []byte

DecodeURLSafe decodes the given URL-safe base64-encoded byte slice. This is a convenience function that creates a new decoder and decodes the input. Returns the decoded data, ignoring any decoding errors.

func Encode added in v1.1.1

func Encode(src []byte) []byte

Encode encodes the given byte slice using standard base64 encoding. This is a convenience function that creates a new encoder and encodes the input.

func EncodeURLSafe added in v1.1.1

func EncodeURLSafe(src []byte) []byte

EncodeURLSafe encodes the given byte slice using URL-safe base64 encoding. This is a convenience function that creates a new encoder and encodes the input.

func NewStreamDecoder

func NewStreamDecoder(r io.Reader, alphabet string) io.Reader

NewStreamDecoder creates a new streaming base64 decoder that reads encoded data from the provided io.Reader. The decoder uses the specified alphabet for decoding. The decoder automatically handles padding and invalid characters.

func NewStreamEncoder

func NewStreamEncoder(w io.Writer, alphabet string) io.WriteCloser

NewStreamEncoder creates a new streaming base64 encoder that writes encoded data to the provided io.Writer. The encoder uses the specified alphabet for encoding. The encoder automatically handles padding when Close() is called.

Types

type AlphabetSizeError

type AlphabetSizeError int

AlphabetSizeError represents an error when the base64 alphabet is invalid. Base64 requires an alphabet of exactly 64 characters for proper encoding and decoding operations. This error occurs when the alphabet length does not meet this requirement.

func (AlphabetSizeError) Error

func (e AlphabetSizeError) Error() string

Error returns a formatted error message describing the invalid alphabet length. The message includes the actual length and the required length for debugging.

type CorruptInputError

type CorruptInputError int64

CorruptInputError represents an error when corrupted or invalid base64 data is detected during decoding. This error occurs when an invalid character is found in the input or when the input data is malformed.

func (CorruptInputError) Error

func (e CorruptInputError) Error() string

Error returns a formatted error message describing the corrupted input. The message includes the position where corruption was detected.

type StdDecoder

type StdDecoder struct {
	Error error // Error field for storing decoding errors
	// contains filtered or unexported fields
}

StdDecoder represents a base64 decoder for standard decoding operations. It wraps the standard library's base64.Encoding to provide a consistent interface with error handling capabilities and support for custom alphabets.

func NewStdDecoder

func NewStdDecoder(alphabet string) *StdDecoder

NewStdDecoder creates a new base64 decoder with the specified alphabet. The alphabet must be a valid base64 alphabet string (exactly 64 characters). Common choices are StdAlphabet for standard decoding or URLAlphabet for URL-safe decoding.

func (*StdDecoder) Decode

func (d *StdDecoder) Decode(src []byte) (dst []byte, err error)

Decode decodes the given base64-encoded byte slice. The decoded result is truncated to the actual decoded length. Handles padding characters (=) automatically according to RFC 4648.

type StdEncoder

type StdEncoder struct {
	Error error // Error field for storing encoding errors
	// contains filtered or unexported fields
}

StdEncoder represents a base64 encoder for standard encoding operations. It wraps the standard library's base64.Encoding to provide a consistent interface with error handling capabilities and support for custom alphabets.

func NewStdEncoder

func NewStdEncoder(alphabet string) *StdEncoder

NewStdEncoder creates a new base64 encoder with the specified alphabet. The alphabet must be a valid base64 alphabet string (exactly 64 characters). Common choices are StdAlphabet for standard encoding or URLAlphabet for URL-safe encoding.

func (*StdEncoder) Encode

func (e *StdEncoder) Encode(src []byte) (dst []byte)

Encode encodes the given byte slice using base64 encoding. The encoded result uses the alphabet specified when creating the encoder. The encoding process handles padding automatically according to RFC 4648.

type StreamDecoder

type StreamDecoder struct {
	Error error // Error field for storing decoding errors
	// contains filtered or unexported fields
}

StreamDecoder represents a streaming base64 decoder that implements io.Reader. It provides efficient decoding for large data streams by processing data in chunks and maintaining an internal buffer for partial reads.

func (*StreamDecoder) Read added in v1.1.1

func (d *StreamDecoder) Read(p []byte) (n int, err error)

Read implements the io.Reader interface for streaming base64 decoding. Reads and decodes base64 data from the underlying reader in chunks. Maintains an internal buffer to handle partial reads efficiently.

type StreamEncoder

type StreamEncoder struct {
	Error error // Error field for storing encoding errors
	// contains filtered or unexported fields
}

StreamEncoder represents a streaming base64 encoder that implements io.WriteCloser. It provides efficient encoding for large data streams by processing data in chunks and writing encoded output immediately.

func (*StreamEncoder) Close added in v1.1.1

func (e *StreamEncoder) Close() error

Close implements the io.Closer interface for streaming base64 encoding. Encodes any remaining buffered bytes from the last Write call. This is the only place where we handle cross-Write state.

func (*StreamEncoder) Write added in v1.1.1

func (e *StreamEncoder) Write(p []byte) (n int, err error)

Write implements the io.Writer interface for streaming base64 encoding. Processes data in chunks while maintaining minimal state for cross-Write calls. This is true streaming - processes data immediately without accumulating large buffers.

Source Files

  • base64.go
  • errors.go

Jump to

Keyboard shortcuts

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