QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
totp.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 KeePassXC Team <team@keepassxc.org>
2// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
3// SPDX-License-Identifier: GPL-3.0-or-later
9#ifndef SRC_TOTP_H_
10#define SRC_TOTP_H_
11
12#include <QByteArray>
13#include <QList>
14#include <QPair>
15#include <QString>
16
17#include <optional>
18
36class Totp {
37public:
41 enum class Algorithm {
42 Sha1,
43 Sha256,
44 Sha512,
45 };
46
50 enum class Encoder {
51 Base10,
52 Steam,
53 };
54
59 enum class Refusal {
60 None,
61 Empty,
62 TooLong,
63 Hotp,
64 UnknownAlgorithm,
65 UnreadableUri,
66 NotASecret,
67 NoSecret,
68 };
69
74 struct Settings {
75 QByteArray key;
76 QString base32Key;
81 QString label;
82 QString issuer;
85 QList<QPair<QString, QString>> extraParams;
89 QString fragment;
90 };
91
92 static constexpr uint DEFAULT_DIGITS = 6U;
93 static constexpr uint DEFAULT_STEP = 30U;
94 static constexpr uint STEAM_DIGITS = 5U;
95 static constexpr uint MIN_DIGITS = 1U;
96 static constexpr uint MAX_DIGITS = 10U;
97 static constexpr uint MAX_STEP = 86400U;
101 static constexpr qsizetype MAX_CONFIG_LENGTH = 4096;
102
128 [[nodiscard]] static auto parse(const QString &config, Refusal *why = nullptr)
129 -> std::optional<Settings>;
130
137 [[nodiscard]] static auto describe(Refusal why) -> QString;
138
144 [[nodiscard]] static auto isValid(const QString &config) -> bool;
145
152 [[nodiscard]] static auto generate(const Settings &settings, quint64 unixTime)
153 -> QString;
154
160 [[nodiscard]] static auto generateNow(const Settings &settings) -> QString;
161
168 [[nodiscard]] static auto counter(const Settings &settings, quint64 unixTime)
169 -> quint64;
170
178 [[nodiscard]] static auto secondsRemaining(const Settings &settings,
179 quint64 unixTime) -> uint;
180
194 [[nodiscard]] static auto toUri(const Settings &settings) -> QString;
195
205 [[nodiscard]] static auto normalize(const QString &config,
206 const QString &label = {},
207 const QString &issuer = {}) -> QString;
208
213 [[nodiscard]] static auto currentUnixTime() -> quint64;
214
215private:
217 Totp() = default;
218};
219
220#endif // SRC_TOTP_H_
Algorithm
HMAC hash function, selected by the algorithm URI parameter.
Definition totp.h:41
@ Sha1
HMAC-SHA-1, the RFC 6238 default.
Definition totp.h:42
static constexpr uint MAX_STEP
Definition totp.h:97
static constexpr uint MIN_DIGITS
Lower clamp for digits.
Definition totp.h:95
static constexpr uint DEFAULT_STEP
RFC 6238 default period.
Definition totp.h:93
static auto counter(const Settings &settings, quint64 unixTime) -> quint64
Time-step counter for a point in time.
Definition totp.cpp:368
static auto generateNow(const Settings &settings) -> QString
Generate the code for the current wall-clock time.
Definition totp.cpp:422
static auto parse(const QString &config, Refusal *why=nullptr) -> std::optional< Settings >
Parse an OTP configuration string.
Definition totp.cpp:264
static constexpr uint DEFAULT_DIGITS
RFC 6238 default code length.
Definition totp.h:92
static constexpr qsizetype MAX_CONFIG_LENGTH
Definition totp.h:101
static constexpr uint STEAM_DIGITS
Steam codes are always 5 chars.
Definition totp.h:94
static auto currentUnixTime() -> quint64
Current wall-clock time.
Definition totp.cpp:484
static auto toUri(const Settings &settings) -> QString
Serialise settings as a canonical otpauth URI.
Definition totp.cpp:426
static auto normalize(const QString &config, const QString &label={}, const QString &issuer={}) -> QString
Normalise a user-entered value to a canonical otpauth URI.
Definition totp.cpp:462
static auto generate(const Settings &settings, quint64 unixTime) -> QString
Generate the code for a given point in time.
Definition totp.cpp:377
static auto describe(Refusal why) -> QString
One sentence for the user on why a configuration gives no code.
Definition totp.cpp:329
Encoder
Digit encoding, selected by the encoder URI parameter.
Definition totp.h:50
@ Base10
RFC 6238 decimal digits, zero padded.
Definition totp.h:51
Refusal
Why parse() gave no settings, so the user can be told more than "invalid".
Definition totp.h:59
static constexpr uint MAX_DIGITS
Upper clamp for digits.
Definition totp.h:96
static auto isValid(const QString &config) -> bool
Convenience validity test, for UI feedback.
Definition totp.cpp:364
static auto secondsRemaining(const Settings &settings, quint64 unixTime) -> uint
Seconds until the code for unixTime expires.
Definition totp.cpp:372
Totp()=default
Static-only class; never instantiated.
Validated TOTP parameters. Only Totp::parse() produces one.
Definition totp.h:74
QString label
otpauth label; may be empty.
Definition totp.h:81
QString issuer
Definition totp.h:82
uint digits
Code length, MIN_DIGITS..MAX_DIGITS.
Definition totp.h:79
QByteArray key
Decoded shared secret; never empty.
Definition totp.h:75
QString base32Key
Sanitized base32 secret, for re-serialisation.
Definition totp.h:76
Algorithm algorithm
HMAC hash function.
Definition totp.h:77
uint step
Period in seconds, 1..MAX_STEP.
Definition totp.h:80
QList< QPair< QString, QString > > extraParams
Definition totp.h:85
QString fragment
Definition totp.h:89
Encoder encoder
Digit encoding.
Definition totp.h:78