std / encoding/base64

encoding/base64

import "std:encoding/base64"

Provides base64 encoding and decoding functions. Supports standard and URL-safe alphabets, with or without padding. Encode/Decode operate on caller-provided buffers (zero-alloc). EncodeToString/DecodeString allocate and return new values.

View source on Codeberg →

Types

#
type Encoding struct {
    alphabet [64]byte
    padChar  byte
}

Encoding holds the base64 alphabet and padding character used for encoding and decoding operations.

Constructors

#
func StdEncoding() Encoding

Returns the standard base64 encoding as defined in RFC 4648, using the standard alphabet and '=' padding.

#
func URLEncoding() Encoding

Returns the URL-safe base64 encoding as defined in RFC 4648, using '-' and '_' instead of '+' and '/', with '=' padding.

#
func StdEncodingNoPad() Encoding

Returns the standard base64 encoding without padding characters.

#
func URLEncodingNoPad() Encoding

Returns the URL-safe base64 encoding without padding characters.

Length

#
func EncodedLen(enc Encoding, n int) int

Returns the length in bytes of the base64 encoding of an input buffer of length n.

#
func DecodedLen(enc Encoding, n int) int

Returns the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data.

Buffer-Based

#
func Encode(enc Encoding, dst []byte, src []byte) int

Encodes src into dst using the given encoding and returns the number of bytes written. dst must be at least EncodedLen(enc, len(src)) bytes long.

#
func Decode(enc Encoding, dst []byte, src []byte) (int, bool)

Decodes src into dst using the given encoding. Returns the number of bytes written to dst and a boolean indicating whether decoding succeeded. dst must be at least DecodedLen(enc, len(src)) bytes long.

Allocating

#
func EncodeToString(enc Encoding, src []byte) string

Returns the base64 encoding of src as a newly allocated string.

#
func DecodeString(enc Encoding, s string) (list[byte], bool)

Returns the bytes represented by the base64 string s as a newly allocated dynamic byte slice, along with a boolean indicating whether decoding succeeded.