🔢 Encodes bytes to a Base32 string using the standard RFC 4648 alphabet.

Info

This is the encoding used for TOTP/2FA shared secrets, which are normally exchanged unpadded — pass { pad: false } for those.

Syntax

TypeScript
import { encodeBase32 } from '@opentf/std';

encodeBase32(
  bytes: Uint8Array | ArrayBuffer,
  options?: { pad?: boolean },
): string;

Parameters

NameTypeDefaultDescription
bytesUint8Array | ArrayBufferThe bytes to encode.
options.padbooleantrueAppend = padding to a multiple of 8 characters.

Returns

The Base32 string, using the alphabet A–Z and 2–7.

Examples

TypeScript
encodeBase32(new Uint8Array([72, 101, 108, 108, 111])) //=> 'JBSWY3DP'
encodeBase32(stringToBytes('foobar'))                  //=> 'MZXW6YTBOI======'

Unpadded, as TOTP secrets are usually shared:

TypeScript
encodeBase32(stringToBytes('foobar'), { pad: false }) //=> 'MZXW6YTBOI'
Last updated on
Edit this page