Skip to main content

encoding/base64

Package base64 implements base64 encoding as specified by RFC 4648. It provides functions for encoding and decoding data using base64 encoding schemes.

Overview

The base64 package provides:
  1. Standard base64 encoding - Using + and / for characters 62 and 63
  2. URL-safe base64 encoding - Using - and _ for characters 62 and 63
  3. Raw (unpadded) variants - Without padding characters
  4. Streaming encoding/decoding - For processing large amounts of data

Constants

Padding

const StdPadding rune = '=' // Standard padding character
const NoPadding rune = -1   // No padding

Variables

Standard Encodings

var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
StdEncoding is the standard base64 encoding, as defined in RFC 4648.
var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
URLEncoding is the alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names.
var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
RawStdEncoding is the standard raw, unpadded base64 encoding, as defined in RFC 4648 section 3.2.
var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names.

Types

Encoding

type Encoding struct {
    // contains filtered or unexported fields
}
An Encoding is a radix 64 encoding/decoding scheme, defined by a 64-character alphabet. The most common encoding is the β€œbase64” encoding defined in RFC 4648.

NewEncoding

func NewEncoding(encoder string) *Encoding
NewEncoding returns a new padded Encoding defined by the given alphabet, which must be a 64-byte string that contains unique byte values.
encoder
string
required
A 64-character string defining the encoding alphabet
*Encoding
*Encoding
A new Encoding instance using the default padding character (’=β€˜)

Methods

Encode
func (enc *Encoding) Encode(dst, src []byte)
Encode encodes src using the encoding enc, writing EncodedLen(len(src)) bytes to dst.
dst
[]byte
required
Destination buffer to write encoded data to. Must have length at least EncodedLen(len(src)).
src
[]byte
required
Source data to encode
Example:
data := []byte("Hello, world!")
dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(dst, data)
fmt.Println(string(dst))
// Output: SGVsbG8sIHdvcmxkIQ==
EncodeToString
func (enc *Encoding) EncodeToString(src []byte) string
EncodeToString returns the base64 encoding of src.
src
[]byte
required
The data to encode
string
string
The base64-encoded string
Example:
msg := "Hello, δΈ–η•Œ"
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Println(encoded)
// Output: SGVsbG8sIOS4lueVjA==
AppendEncode
func (enc *Encoding) AppendEncode(dst, src []byte) []byte
AppendEncode appends the base64 encoded src to dst and returns the extended buffer.
dst
[]byte
required
Destination buffer to append to
src
[]byte
required
Source data to encode and append
[]byte
[]byte
The extended buffer containing dst followed by the encoded src
Decode
func (enc *Encoding) Decode(dst, src []byte) (n int, err error)
Decode decodes src using the encoding enc. It writes at most DecodedLen(len(src)) bytes to dst and returns the number of bytes written.
dst
[]byte
required
Destination buffer to write decoded data to
src
[]byte
required
Base64-encoded source data
n
int
The number of bytes written to dst
err
error
Returns CorruptInputError if the input is not valid base64
Example:
str := "SGVsbG8sIHdvcmxkIQ=="
dst := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
n, err := base64.StdEncoding.Decode(dst, []byte(str))
if err != nil {
    fmt.Println("decode error:", err)
    return
}
dst = dst[:n]
fmt.Printf("%q\n", dst)
// Output: "Hello, world!"
DecodeString
func (enc *Encoding) DecodeString(s string) ([]byte, error)
DecodeString returns the bytes represented by the base64 string s.
s
string
required
The base64-encoded string to decode
[]byte
[]byte
The decoded data
error
error
Returns CorruptInputError if the input is not valid base64
Example:
str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
    fmt.Println("error:", err)
    return
}
fmt.Printf("%q\n", data)
// Output: "some data with \x00 and \ufeff"
AppendDecode
func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error)
AppendDecode appends the base64 decoded src to dst and returns the extended buffer.
dst
[]byte
required
Destination buffer to append to
src
[]byte
required
Base64-encoded source data to decode and append
[]byte
[]byte
The extended buffer containing dst followed by the decoded src
error
error
Returns CorruptInputError if the input is not valid base64
EncodedLen
func (enc *Encoding) EncodedLen(n int) int
EncodedLen returns the length in bytes of the base64 encoding of an input buffer of length n.
n
int
required
The length of the input data
int
int
The length of the encoded output
DecodedLen
func (enc *Encoding) DecodedLen(n int) int
DecodedLen returns the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data.
n
int
required
The length of the encoded data
int
int
The maximum length of the decoded output
WithPadding
func (enc Encoding) WithPadding(padding rune) *Encoding
WithPadding creates a new encoding identical to enc except with a specified padding character, or NoPadding to disable padding.
padding
rune
required
The padding character to use, or NoPadding (-1) to disable padding
*Encoding
*Encoding
A new Encoding instance with the specified padding
Strict
func (enc Encoding) Strict() *Encoding
Strict creates a new encoding identical to enc except with strict decoding enabled. In this mode, the decoder requires that trailing padding bits are zero.
*Encoding
*Encoding
A new Encoding instance with strict decoding enabled

Streaming Encoder

NewEncoder

func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
NewEncoder returns a new base64 stream encoder. Data written to the returned writer will be encoded using enc and then written to w. Base64 encodings operate in 4-byte blocks; when finished writing, the caller must Close the returned encoder to flush any partially written blocks.
enc
*Encoding
required
The encoding to use
w
io.Writer
required
The writer to write encoded data to
io.WriteCloser
io.WriteCloser
A WriteCloser that encodes data written to it
Example:
input := []byte("foo\x00bar")
encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
encoder.Write(input)
// Must close the encoder when finished to flush any partial blocks
encoder.Close()
// Output: Zm9vAGJhcg==

Streaming Decoder

NewDecoder

func NewDecoder(enc *Encoding, r io.Reader) io.Reader
NewDecoder constructs a new base64 stream decoder.
enc
*Encoding
required
The encoding to use
r
io.Reader
required
The reader to read encoded data from
io.Reader
io.Reader
A Reader that decodes data read from it

Error Types

CorruptInputError

type CorruptInputError int64
CorruptInputError indicates that the input data is corrupted. The int64 value indicates the byte offset where the corruption was detected.
func (e CorruptInputError) Error() string

Encoding Examples

Standard Base64

// Encoding
data := []byte("any + old & data")
str := base64.StdEncoding.EncodeToString(data)
fmt.Println(str)
// Output: YW55ICsgb2xkICYgZGF0YQ==

// Decoding
str := "SGVsbG8sIOS4lueVjA=="
decoded, err := base64.StdEncoding.DecodeString(str)
if err != nil {
    fmt.Println("decode error:", err)
    return
}
fmt.Println(string(decoded))
// Output: Hello, δΈ–η•Œ

URL-Safe Base64

// Use URLEncoding for URLs and filenames
data := []byte("test?data=value&param=123")
encoded := base64.URLEncoding.EncodeToString(data)
fmt.Println(encoded)

decoded, _ := base64.URLEncoding.DecodeString(encoded)
fmt.Println(string(decoded))

Raw (Unpadded) Base64

// Use RawStdEncoding for base64 without padding
data := []byte("hello")
encoded := base64.RawStdEncoding.EncodeToString(data)
fmt.Println(encoded)
// Output: aGVsbG8 (no padding)

// Use RawURLEncoding for URL-safe base64 without padding
encoded = base64.RawURLEncoding.EncodeToString(data)
fmt.Println(encoded)

Custom Encoding

// Create a custom encoding with different alphabet
customEnc := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
encoded := customEnc.EncodeToString([]byte("test"))

// Create encoding without padding
noPadEnc := customEnc.WithPadding(base64.NoPadding)
encoded = noPadEnc.EncodeToString([]byte("test"))

Build docs developers (and LLMs) love