QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
Base32 Class Reference

RFC 4648 base32 codec, used for TOTP shared secrets. More...

#include <base32.h>

Static Public Member Functions

static auto decode (const QByteArray &encodedData) -> QByteArray
 Decode strict RFC 4648 base32 data.
static auto encode (const QByteArray &data) -> QByteArray
 Encode raw bytes as padded RFC 4648 base32.
static auto addPadding (const QByteArray &encodedData) -> QByteArray
 Append '=' padding up to a multiple of 8 characters.
static auto removePadding (const QByteArray &encodedData) -> QByteArray
 Strip trailing '=' padding.
static auto sanitizeInput (const QByteArray &encodedData) -> QByteArray
 Make human-entered base32 text decodable.

Private Member Functions

 Base32 ()=default
 Static-only class; never instantiated.

Detailed Description

RFC 4648 base32 codec, used for TOTP shared secrets.

Ported from KeePassXC (src/core/Base32.cpp). decode() is deliberately strict: the input length must be a multiple of 8 and every character must be in the base32 alphabet, so whitespace is an error rather than something to skip. Secrets that come from a human (or from a QR code label) therefore go through sanitizeInput() first, making the canonical entry point:

const QByteArray secret =
static auto sanitizeInput(const QByteArray &encodedData) -> QByteArray
Make human-entered base32 text decodable.
Definition base32.cpp:305
static auto decode(const QByteArray &encodedData) -> QByteArray
Decode strict RFC 4648 base32 data.
Definition base32.cpp:128

Failure is reported as an empty QByteArray. A successful decode of a non-empty input always yields at least one byte, so isEmpty() is an unambiguous error test.

Definition at line 34 of file base32.h.

Member Function Documentation

◆ addPadding()

auto Base32::addPadding ( const QByteArray & encodedData) -> QByteArray
staticnodiscard

Append '=' padding up to a multiple of 8 characters.

Parameters
encodedDataUnpadded or partially padded base32 text.
Returns
Padded text, or encodedData unchanged when its length remainder is impossible for base32 (1, 3 or 6).

Definition at line 275 of file base32.cpp.

Here is the caller graph for this function:

◆ decode()

auto Base32::decode ( const QByteArray & encodedData) -> QByteArray
staticnodiscard

Decode strict RFC 4648 base32 data.

Parameters
encodedDataPadded base32 text whose length is a multiple of 8.
Returns
Decoded bytes, or an empty QByteArray when the input is empty, mis-padded, or contains a character outside "A-Za-z2-7=".

Definition at line 128 of file base32.cpp.

Here is the caller graph for this function:

◆ encode()

auto Base32::encode ( const QByteArray & data) -> QByteArray
staticnodiscard

Encode raw bytes as padded RFC 4648 base32.

Parameters
dataRaw bytes to encode.
Returns
Upper-case padded base32 text, or an empty QByteArray when data is empty.

Definition at line 191 of file base32.cpp.

◆ removePadding()

auto Base32::removePadding ( const QByteArray & encodedData) -> QByteArray
staticnodiscard

Strip trailing '=' padding.

Parameters
encodedDataPadded base32 text.
Returns
Text without padding, or encodedData unchanged when its length is not a multiple of 8.

Definition at line 294 of file base32.cpp.

Here is the caller graph for this function:

◆ sanitizeInput()

auto Base32::sanitizeInput ( const QByteArray & encodedData) -> QByteArray
staticnodiscard

Make human-entered base32 text decodable.

Drops every character outside "A-Za-z2-7" (so the spaces and dashes used to group a printed secret are tolerated), maps the digits that are ambiguous in the base32 alphabet (0 to O, 1 to L, 8 to B), then pads the result with addPadding().

Parameters
encodedDataRaw user input.
Returns
Sanitized, padded base32 text.

Definition at line 305 of file base32.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: