Skip to content

Core\Encoding::fromBase64

Core\Encoding::fromBase64(string $s): bytesCore\Encoding::fromBase64(
    string $s,
): bytes

Reads base64 text back into the bytes it was made from.

Base64 writes any bytes as text, using the letters A to Z in both cases, the digits 0 to 9, and the two symbols + and /. Programs use it wherever only text may travel: an HTTP header, an email attachment, a small image inside a stylesheet. Core\Encoding::fromBase64 is the other half of Core\Encoding::toBase64. It reads one exact form: the alphabet above, and = padding where the last group is short. A space, a newline, or a text that stops part way through a group is an error. The error says what is wrong and at which offset.

Good to know: text made for a URL or a JSON Web Token uses - and _ in place of + and /. Core\Encoding::fromBase64Url is the member that reads that form.

Parameters

  • $sstringThe base64 text.

Return value

Returns bytes. The decoded bytes. The empty string gives empty bytes.

Errors

  • RuntimeError$s is not strict base64. It has a symbol outside the alphabet, such as - or _, missing or wrong padding, an incomplete last group, or unused bits that are not zero.

Examples

The same bytes after the round trip

<?nvs
// Shows the round trip: bytes go out as base64 text, and the same bytes come
// back.
// The first eight bytes of every PNG file.
bytes $signature = Core\Encoding::fromHex('89504e470d0a1a0a');
// `Core\Encoding::toBase64` writes the text. This prints iVBORw0KGgo=.
string $written = Core\Encoding::toBase64($signature);
echo $written, "\n";
// `Core\Encoding::fromBase64` reads it back. This prints 89504e470d0a1a0a,
// the hex we started with.
echo Core\Encoding::toHex(Core\Encoding::fromBase64($written)), "\n";
// The empty string gives an empty buffer. This prints 0.
echo Core\Bytes::length(Core\Encoding::fromBase64('')), "\n";
Output
iVBORw0KGgo=
89504e470d0a1a0a
0

The one form it reads

<?nvs
// Shows the one form `Core\Encoding::fromBase64` reads, and what to do with
// text that is close to it and not in it.
// Four symbols carry three bytes. This prints 616263, the bytes of "abc".
echo Core\Encoding::toHex(Core\Encoding::fromBase64('YWJj')), "\n";
// A last group of fewer than three bytes is filled up with `=`. This prints
// 61626364.
echo Core\Encoding::toHex(Core\Encoding::fromBase64('YWJjZA==')), "\n";
// Text made for a URL uses `-` and `_` where this alphabet uses `+` and `/`.
// `Core\Encoding::fromBase64Url` reads that form.
try {
Core\Encoding::fromBase64('aGVsbG8-d29ybGQ_');
} catch (RuntimeError $urlSafe) {
echo 'refused: ', $urlSafe->message, "\n";
}
// Long base64 in an email is written over several lines. A newline is not a
// base64 symbol, so take the newlines out first with `Core\Str::replace`.
string $wrapped = "YWJjZGVmZ2hp\namtsbW5vcHFy\nc3R1dnd4eXo=";
bytes $joined = Core\Encoding::fromBase64(Core\Str::replace($wrapped, "\n", ''));
// This prints abcdefghijklmnopqrstuvwxyz.
echo Core\Encoding::decodeText($joined, Core\Charset::Utf8), "\n";
// The `=` at the end belongs to the text and may not be left off.
try {
Core\Encoding::fromBase64('YWJ');
} catch (RuntimeError $unpadded) {
echo 'refused: ', $unpadded->message, "\n";
}
Output
616263
61626364
refused: Core\Encoding::fromBase64(): "aGVsbG8-d29ybGQ_" is not base64 — the byte 0x2d at offset 7 is not a base64 character, and whitespace, a newline and a misplaced `=` are each that byte rather than something skipped
abcdefghijklmnopqrstuvwxyz
refused: Core\Encoding::fromBase64(): "YWJ" is not base64 — its `=` padding is missing or malformed, and the standard form is written with it

A basic authorization header

<?nvs
// Shows the everyday use: an HTTP `Authorization: Basic` header carries a user
// name and a password as base64.
string $header = 'Basic YWRhOmh1bnRlcjI=';
// Everything after "Basic " is the base64 text. This prints ada:hunter2.
string $pair = Core\Encoding::decodeText(
Core\Encoding::fromBase64(Core\Str::slice($header, 6, null)),
Core\Charset::Utf8,
);
echo $pair, "\n";
// The first `:` separates the two. This prints ada and then hunter2.
echo Core\Str::before($pair, ':'), "\n";
echo Core\Str::after($pair, ':'), "\n";
// The bytes behind the base64 are not always text. Here the password is one
// byte that UTF-8 has no character for, so `Core\Encoding::decodeText` throws.
try {
bytes $raw = Core\Encoding::fromBase64('YWRhOv8=');
echo Core\Encoding::decodeText($raw, Core\Charset::Utf8), "\n";
} catch (RuntimeError $notText) {
echo 'refused: ', $notText->message, "\n";
}
Output
ada:hunter2
ada
hunter2
refused: a conversion is exact or it throws: Core\Encoding::decodeText() found at offset 4 a byte sequence that is not `Core\Charset::Utf8`, and nothing is replaced with U+FFFD or dropped