Skip to content

Core\Encoding::fromBase32

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

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

Base32 writes any bytes using only the letters A to Z and the digits 2 to 7. Those symbols survive being typed by hand, read out loud or put in a URL, which is why the shared secret in a two-factor authentication link is written this way. Core\Encoding::fromBase32 is the other half of Core\Encoding::toBase32. It accepts upper case and lower case, and = padding or none at all, because neither of those changes which bytes come out. Text that is not base32 throws a RuntimeError saying what is wrong with it and where.

Good to know: base32 takes more room than base64 for the same bytes. Use base64 when only programs read the text, and base32 when a person has to handle it.

Parameters

  • $sstringThe base32 text.

Return value

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

Errors

  • RuntimeError$s has a symbol outside the alphabet, such as a space between groups. Or it has an incomplete last group, unused bits that are not zero, or wrong padding.

Examples

Back to the bytes you started with

<?nvs
// Shows the round trip: bytes go out as base32 text, and the same bytes come
// back.
bytes $key = Core\Encoding::fromHex('68656c6c6f');
// `Core\Encoding::toBase32` writes the text. This prints NBSWY3DP.
string $written = Core\Encoding::toBase32($key);
echo $written, "\n";
// `Core\Encoding::fromBase32` reads it back. This prints 68656c6c6f, the hex
// we started with.
echo Core\Encoding::toHex(Core\Encoding::fromBase32($written)), "\n";
// The empty string gives an empty buffer. This prints 0.
echo Core\Bytes::length(Core\Encoding::fromBase32('')), "\n";
Output
NBSWY3DP
68656c6c6f
0

What the reader may change and what they may not

<?nvs
// Shows what a person may change about base32 text without changing the bytes,
// and what they may not.
// Lower case gives the same bytes as upper case. Both lines print 666f6f.
echo Core\Encoding::toHex(Core\Encoding::fromBase32('MZXW6')), "\n";
echo Core\Encoding::toHex(Core\Encoding::fromBase32('mzxw6')), "\n";
// Padding may be written in full or left off. Both lines print 666f6f too.
echo Core\Encoding::toHex(Core\Encoding::fromBase32('MZXW6===')), "\n";
echo Core\Encoding::toHex(Core\Encoding::fromBase32('MZXW6')), "\n";
// A space between groups is not allowed. Take the spaces out first with
// `Core\Str::replace`.
try {
Core\Encoding::fromBase32('MZXW YTB');
} catch (RuntimeError $spaced) {
echo 'refused: ', $spaced->message, "\n";
}
// The digits 0, 1 and 8 are not in the base32 alphabet. A reader who typed a
// zero for the letter O gets an error.
try {
Core\Encoding::fromBase32('MZXW0YTB');
} catch (RuntimeError $mistyped) {
echo 'refused: ', $mistyped->message, "\n";
}
Output
666f6f
666f6f
666f6f
666f6f
refused: Core\Encoding::fromBase32(): "MZXW YTB" is not base32 — the byte at offset 4 is not one of `A`-`Z` or `2`-`7` — the digits `0`, `1` and `8` are not in this alphabet, and a space grouping a secret for a reader is a byte like any other
refused: Core\Encoding::fromBase32(): "MZXW0YTB" is not base32 — the byte at offset 4 is not one of `A`-`Z` or `2`-`7` — the digits `0`, `1` and `8` are not in this alphabet, and a space grouping a secret for a reader is a byte like any other

A two factor secret from a link

<?nvs
// Shows the everyday use: a two-factor authentication link carries its shared
// secret as base32, and your program needs the bytes behind it.
// The `secret=` part of an `otpauth://totp/…` link.
string $fromTheLink = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP';
// Read it back to the bytes it stands for. This prints 20, the number of bytes
// RFC 4226 recommends for a secret.
bytes $secret = Core\Encoding::fromBase32($fromTheLink);
echo Core\Bytes::length($secret), "\n";
// Those bytes are what a code is made from. A code is six digits and changes
// every thirty seconds, so this prints 6.
echo Core\Str::length(Core\Totp::code($secret)), "\n";
// An authenticator app shows the same secret in groups of four. The spaces are
// not base32, so take them out first. This prints same bytes.
string $asShown = 'JBSW Y3DP EHPK 3PXP JBSW Y3DP EHPK 3PXP';
bytes $typedBack = Core\Encoding::fromBase32(Core\Str::replace($asShown, ' ', ''));
echo (Core\Bytes::compare($typedBack, $secret) == 0 ? 'same bytes' : 'different bytes'), "\n";
Output
20
6
same bytes