Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

1.5 KB
Raw Blame
#pragma once
// Constant-time comparison helpers for secrets.
//
// Any comparison of a user- or network-supplied value against a secret (PIN
// hash, bridge token, OAuth state nonce) must not short-circuit on the first
// differing byte: the timing difference leaks how many leading bytes were
// correct, which turns an infeasible guess into a byte-at-a-time search.
//
// Header-only so the loopback HTTP bridges (services/wallet/*, mcp/*) and the
// auth layer share one implementation instead of each rolling their own.

#include <QByteArray>
#include <QString>

namespace fincept::auth {

/// Returns true iff `a` and `b` are the same length AND every byte matches.
/// The XOR-accumulate loop touches every byte even on mismatch so execution
/// time does not reveal the first differing index.
///
/// Length is compared first and therefore still observable — that is inherent
/// and harmless for the fixed-width secrets we compare.
inline bool constant_time_equals(const QByteArray& a, const QByteArray& b) {
    if (a.size() != b.size())
        return false;
    unsigned char diff = 0;
    const qsizetype n = a.size();
    for (qsizetype i = 0; i < n; ++i)
        diff |= static_cast<unsigned char>(a[i]) ^ static_cast<unsigned char>(b[i]);
    return diff == 0;
}

/// UTF-8 overload for QString secrets (tokens, nonces).
inline bool constant_time_equals(const QString& a, const QString& b) {
    return constant_time_equals(a.toUtf8(), b.toUtf8());
}

} // namespace fincept::auth