Skip to content

Core\Encoding::fromBase64Url

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

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

URL-safe 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 _. All of those are safe in a web address, in a file name and in a JSON Web Token, which is where you meet this form. Core\Encoding::fromBase64Url is the other half of Core\Encoding::toBase64Url. It reads one exact form: the alphabet above, and no = padding at all. A +, a /, an =, 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: one member reads one form. Text written with +, / and = is Core\Encoding::fromBase64's to read.

Parameters

  • $sstringThe URL-safe base64 text, with no padding.

Return value

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

Errors

  • RuntimeError$s is not URL-safe base64 without padding. It has a symbol outside the alphabet, such as +, / or =, an incomplete last group, or unused bits that are not zero.

Examples

Text that is safe in a web address

<?nvs
// Shows the round trip: bytes go out as URL-safe base64 text, and the same
// bytes come back.
// A short text whose characters all need care in a web address.
bytes $raw = Core\Encoding::encodeText('~hello?world/ok+fine~', Core\Charset::Utf8);
// `Core\Encoding::toBase64Url` writes the text. This prints
// fmhlbGxvP3dvcmxkL29rK2ZpbmV-, which a web address carries unchanged.
string $written = Core\Encoding::toBase64Url($raw);
echo $written, "\n";
// `Core\Encoding::fromBase64Url` reads it back. This prints the text we
// started with.
echo Core\Encoding::decodeText(Core\Encoding::fromBase64Url($written), Core\Charset::Utf8), "\n";
// The empty string gives an empty buffer. This prints 0.
echo Core\Bytes::length(Core\Encoding::fromBase64Url('')), "\n";
Output
fmhlbGxvP3dvcmxkL29rK2ZpbmV-
~hello?world/ok+fine~
0

The two differences from the other form

<?nvs
// Shows the two things that make this form different from `fromBase64`, and
// what happens when text from that other form arrives here.
// `-` and `_` stand where the other form writes `+` and `/`. This prints
// 68656c6c6f3e776f726c643f.
echo Core\Encoding::toHex(Core\Encoding::fromBase64Url('aGVsbG8-d29ybGQ_')), "\n";
// There is no padding. A last group of one or two bytes simply stops, so
// these two lines print 616263 and 6162.
echo Core\Encoding::toHex(Core\Encoding::fromBase64Url('YWJj')), "\n";
echo Core\Encoding::toHex(Core\Encoding::fromBase64Url('YWI')), "\n";
// An `=` at the end is not part of this form.
try {
Core\Encoding::fromBase64Url('YWJjZA==');
} catch (RuntimeError $padded) {
echo 'refused: ', $padded->message, "\n";
}
// Neither are `+` and `/`. Text written that way is
// `Core\Encoding::fromBase64`'s to read.
try {
Core\Encoding::fromBase64Url('aGVsbG8+d29ybGQ/');
} catch (RuntimeError $standard) {
echo 'refused: ', $standard->message, "\n";
}
Output
68656c6c6f3e776f726c643f
616263
6162
refused: Core\Encoding::fromBase64Url(): "YWJjZA==" is not URL-safe base64 — it is padded, and the URL-safe form is written without `=`
refused: Core\Encoding::fromBase64Url(): "aGVsbG8+d29ybGQ/" is not URL-safe base64 — the byte 0x2b at offset 7 is not a base64url character, and whitespace, a newline and a misplaced `=` are each that byte rather than something skipped

A json web token

<?nvs
// Shows the everyday use: the parts of a JSON Web Token are URL-safe base64,
// and your program reads what is inside them.
string $jwt = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZGEiLCJhZG1pbiI6dHJ1ZX0.c2lnbmF0dXJl';
// The three parts are separated by `.`. This prints 3.
array<string> $parts = Core\Str::split($jwt, '.');
echo Core\Arr::count($parts), "\n";
// The first part says how the token was signed. This prints {"alg":"HS256"}.
echo Core\Encoding::decodeText(Core\Encoding::fromBase64Url($parts[0]), Core\Charset::Utf8), "\n";
// The second part carries the claims. This prints {"sub":"ada","admin":true}.
echo Core\Encoding::decodeText(Core\Encoding::fromBase64Url($parts[1]), Core\Charset::Utf8), "\n";
// Reading a token is not checking it. The third part is the signature, and it
// has to be verified before you use anything above. This prints 9.
echo Core\Bytes::length(Core\Encoding::fromBase64Url($parts[2])), "\n";
Output
3
{"alg":"HS256"}
{"sub":"ada","admin":true}
9