Documentation
¶
Overview ¶
Package base64 implements the base64 codec (RFC 4648) without the Go standard library.
It exists because `encoding/base64` costs a measured 18,740 bytes in a TinyGo wasm binary — real weight on the edge (Cloudflare Workers, goflare) for a transformation that is a lookup table and some bit shifting. The stdlib is TinyGo-compatible; this package is about size, not compatibility.
Index ¶
Constants ¶
const ( ErrAlphabetLength = Error("base64: el alfabeto debe tener exactamente 64 caracteres") ErrAlphabetDuplicate = Error("base64: el alfabeto tiene caracteres repetidos") ErrAlphabetNonASCII = Error("base64: el alfabeto sólo admite ASCII") ErrInvalidCharacter = Error("base64: carácter no válido en la entrada") ErrInvalidLength = Error("base64: longitud de entrada no válida") )
Variables ¶
var ErrInvalid error = invalidError{}
ErrInvalid is returned for any input that is not well-formed base64.
Functions ¶
func Decode ¶ added in v0.0.4
Decode decodes a padded standard base64 string (RFC 4648 §4), equivalent to the stdlib's StdEncoding.Strict(). Padding is required and its length must match the trailing group size; any '=' outside the final group — or any byte outside the standard alphabet — is rejected via the same canonicality rule URLDecode applies.
func Encode ¶ added in v0.0.4
Encode encodes src as standard base64 (RFC 4648 §4) WITH padding — equivalent to the stdlib's StdEncoding. This is what data URIs, MCP image content, and most JSON/HTTP payloads expect; use URLEncode instead for tokens embedded in a URL or a JWT segment.
func URLDecode ¶ added in v0.0.2
URLDecode decodes an unpadded base64url string.
Every byte outside the alphabet is rejected, including '=', '+', '/' and whitespace, and so is any non-canonical encoding (RFC 4648 §3.5: the unused trailing bits of the final group must be zero). This decodes tokens, so leniency would mean accepting a signature segment the signer never produced. It is equivalent to the stdlib's RawURLEncoding.Strict() — deliberately stricter than the stdlib default, which accepts non-canonical input.
Types ¶
type Encoding ¶ added in v0.0.5
type Encoding struct {
// contains filtered or unexported fields
}
Encoding es un códec base64 con alfabeto propio. Se construye una vez y se reutiliza; NewEncoding precalcula la tabla inversa para que decodificar no tenga que recorrer el alfabeto por cada carácter.
func NewEncoding ¶ added in v0.0.5
NewEncoding devuelve un códec sobre alphabet, que DEBE tener exactamente 64 caracteres ASCII distintos. pad indica si la salida lleva relleno '='.
func (*Encoding) Decode ¶ added in v0.0.5
Decode decodifica s con el alfabeto del Encoding. Es estricta: rechaza caracteres fuera del alfabeto, longitudes imposibles, relleno incorrecto y codificaciones no canónicas (bits sobrantes no cero, RFC 4648 §3.5).
func (*Encoding) DecodedLen ¶ added in v0.0.5
DecodedLen devuelve la longitud máxima en bytes de la decodificación de n bytes codificados. Para pad=true asume entrada con relleno; para pad=false asume entrada sin relleno.
func (*Encoding) EncodedLen ¶ added in v0.0.5
EncodedLen devuelve la longitud en bytes de la codificación de n bytes de entrada.