Skip to content

Core\Encoding::toBase32

Writes bytes as base32 text, which a person can type, read out loud or put in a URL.

Base32 writes any bytes using only the upper-case letters A to Z and the digits 2 to 7. Every five bytes become eight characters. Core\Encoding::toBase32 writes no = padding at the end, so a short last group simply gives fewer characters. Every byte has a spelling, so this method never fails, and the empty buffer gives the empty string.

The shared secret in a two-factor authentication link is written this way. Core\Encoding::fromBase32 reads the text back into the same bytes.

Good to know: some older tools expect = padding, so that the length divides by eight. Core\Str::padEnd adds it.

Parameters

  • $bbytesThe bytes to encode.

Return value

Returns string. The base32 text. Empty bytes give the empty string.

Examples

Five bytes make eight characters

<?nvs
// Shows how the text grows with the bytes. Every five bytes become eight
// characters, and nothing is added at the end.
// These are the test values from the base32 standard, RFC 4648.
// This prints MY, MZXQ, MZXW6, MZXW6YQ and MZXW6YTB.
array<string> $words = ['f', 'fo', 'foo', 'foob', 'fooba'];
foreach ($words as string $word) {
echo Core\Encoding::toBase32(Core\Encoding::encodeText($word, Core\Charset::Utf8)), "\n";
}
// A sixth byte starts a new group. This prints MZXW6YTBOI.
echo Core\Encoding::toBase32(Core\Encoding::encodeText('foobar', Core\Charset::Utf8)), "\n";
// A tool that expects padding needs `=` until the length divides by eight.
// `Core\Str::padEnd` adds it. This prints MZXW6YQ=.
echo Core\Str::padEnd(Core\Encoding::toBase32(Core\Encoding::fromHex('666f6f62')), 8, '='), "\n";
// The empty buffer gives the empty string, so this line prints nothing.
echo Core\Encoding::toBase32(Core\Encoding::encodeText('', Core\Charset::Utf8)), "\n";
Output
MY
MZXQ
MZXW6
MZXW6YQ
MZXW6YTB
MZXW6YTBOI
MZXW6YQ=

Only letters and digits

<?nvs
// Shows base32 text next to base64 text for the same bytes.
bytes $raw = Core\Encoding::fromHex('fbff00');
// Base64 uses both cases and the symbols `+` and `/`. This prints +/8A.
echo Core\Encoding::toBase64($raw), "\n";
// Base32 uses upper-case letters and the digits 2 to 7 only. This prints 7P7QA.
echo Core\Encoding::toBase32($raw), "\n";
// The digits 0, 1, 8 and 9 are not used. So a reader cannot mix up 0 and O,
// or 1 and I. Any bytes give the same result. This prints no confusing digits.
string $all = Core\Encoding::toBase32(Core\Random::bytes(300));
bool $mixed = Core\Str::contains($all, '0')
|| Core\Str::contains($all, '1')
|| Core\Str::contains($all, '8')
|| Core\Str::contains($all, '9');
echo ($mixed ? 'confusing digits' : 'no confusing digits'), "\n";
// Base32 is longer. Twenty bytes give 32 characters in base32 and 28 in
// base64. This prints 32 28.
bytes $key = Core\Bytes::fill(20, 200);
echo Core\Str::length(Core\Encoding::toBase32($key)), ' ', Core\Str::length(Core\Encoding::toBase64($key)), "\n";
Output
+/8A
7P7QA
no confusing digits
32 28

A new two factor secret

<?nvs
// Shows the everyday use: you make a new two-factor secret for a user, and put
// it in the link that their authenticator app reads.
// Twenty random bytes are the secret. RFC 4226 recommends this size.
bytes $secret = Core\Random::bytes(20);
// Base32 writes them as 32 characters. This prints 32.
string $written = Core\Encoding::toBase32($secret);
echo Core\Str::length($written), "\n";
// The text goes in the `secret=` part of the link. `toBase32` writes no `=`
// padding, so the link has only these two `=` signs. This prints 2.
string $link = 'otpauth://totp/Shop:ada@example.com?secret=' . $written . '&issuer=Shop';
echo Core\Str::countOf($link, '='), "\n";
// Some people type the secret instead of scanning the link. Apps show it in
// groups of four. This prints 39: 32 characters and 7 spaces.
string $shown = Core\Str::join(Core\Str::chunk($written, 4), ' ');
echo Core\Str::length($shown), "\n";
// The app reads the text back into the same bytes. This prints 0.
echo Core\Bytes::compare(Core\Encoding::fromBase32($written), $secret), "\n";
Output
32
2
39
0