Base32 Encoder and Decoder

// Using the base32 library
const base32 = require("base32");

const encoded = base32.encode(message);
const decoded = base32.decode(encoded);

console.log(`Base32 encoding of ${message}: ${encoded}`);
console.log(`Base32 decoding of ${encoded}: ${decoded}`);

The Base32 encoding

Base32 is an encoding comprised of 32 separate characters. Generally, Base32 represents byte strings, in the same way that hexadecimal might. Base32 has a number of advantages as an encoding. For one, it is much more space efficient than hexadecimal, taking up about 20% of the space that hexadecimal might.

It also:

  • is entirely one case, making dictation over the phone or human memory easier
  • can be written into a URL without percent-encoding
  • can be a filename
  • often omits easily confusable character pairs (1 & I, 0 & O, 8 & B)

This makes Base32 a great choice when raw data needs to be passed between people. However, its cousin, Base64, is more effective when arbitrary data needs to be represented using a limited character set because of its efficient use of space.

Padding & the alphabet

In some Base32 implementations, a padding character is provisioned to ensure that the Base32 string is the correct length. However, padding is omitted in other implementations since it can often be inferred from the source string.

A Base32 alphabet uses a set of 32 digits to represent 5 bit values (25). An alphabet can be designed to maximize performance and readability, ensure cohesion with other systems, or be otherwise tailored for a specific use case. Pictured below is the original RFC 4648 Base32 alphabet, though other popular alphabets include z-base-32, Crockford's Base32, and base32hex.

Base32 alphabet defined by RFC 4648