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:
- Standard base64 encoding - Using + and / for characters 62 and 63
- URL-safe base64 encoding - Using - and _ for characters 62 and 63
- Raw (unpadded) variants - Without padding characters
- 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.
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.
A 64-character string defining the encoding alphabet
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.
Destination buffer to write encoded data to. Must have length at least EncodedLen(len(src)).
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.
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.
Destination buffer to append to
Source data to encode and append
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.
Destination buffer to write decoded data to
Base64-encoded source data
The number of bytes written to dst
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.
The base64-encoded string to decode
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.
Destination buffer to append to
Base64-encoded source data to decode and append
The extended buffer containing dst followed by the decoded src
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.
The length of the input data
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.
The length of the encoded data
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.
The padding character to use, or NoPadding (-1) to disable padding
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.
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.
The writer to write encoded data to
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.
The reader to read encoded data from
A Reader that decodes data read from it
Error Types
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¶m=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"))