libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
totp.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
24#pragma once
25
26#include <cstdint>
27#include <ctime>
28#include <optional>
29#include <string>
30
31#include "libkeepass/export.hh"
32#include "libkeepass/secure.hh"
33
34namespace keepass {
35
36class Entry;
37
51public:
53 enum class Algorithm { kSha1, kSha256, kSha512 };
54
55private:
56 secure_string secret_;
57 uint32_t period_ = 30;
58 uint32_t digits_ = 6;
59 Algorithm algorithm_ = Algorithm::kSha1;
60
61public:
63 TotpConfig() = default;
64
71 explicit TotpConfig(secure_string secret, uint32_t period = 30, uint32_t digits = 6,
72 Algorithm algorithm = Algorithm::kSha1);
73
75 const secure_string& secret() const { return secret_; }
76
78 void set_secret(const secure_string& secret) { secret_ = secret; }
79
81 uint32_t period() const { return period_; }
82
84 void set_period(uint32_t period) { period_ = period == 0 ? 1 : period; }
85
87 uint32_t digits() const { return digits_; }
88
90 void set_digits(uint32_t digits) { digits_ = digits < 1 ? 1 : (digits > 10 ? 10 : digits); }
91
93 Algorithm algorithm() const { return algorithm_; }
94
96 void set_algorithm(Algorithm algorithm) { algorithm_ = algorithm; }
97
99 bool empty() const { return secret_.empty(); }
100
109 static std::optional<TotpConfig> Parse(const std::string& seed_field,
110 const std::string& settings_field = {});
111
113 static std::optional<TotpConfig> FromEntry(const Entry& entry);
114};
115
119class LIBKEEPASS_API Totp final {
120public:
128 static std::string Generate(const TotpConfig& config, std::time_t time = 0);
129
131
136 static std::string Generate(const Entry& entry, std::time_t time = 0);
137
139
144 static uint32_t SecondsRemaining(const TotpConfig& config, std::time_t time = 0);
145};
146
147} // namespace keepass
A single KeePass entry (a username/password record).
Definition entry.hh:55
Parsed TOTP parameters from an entry's custom fields.
Definition totp.hh:50
static std::optional< TotpConfig > FromEntry(const Entry &entry)
Extracts and parses the TOTP fields of an entry.
void set_period(uint32_t period)
Sets the time step in seconds (clamped to at least one second).
Definition totp.hh:84
TotpConfig()=default
Creates an empty (invalid) configuration.
TotpConfig(secure_string secret, uint32_t period=30, uint32_t digits=6, Algorithm algorithm=Algorithm::kSha1)
void set_secret(const secure_string &secret)
Replaces the Base32 shared secret.
Definition totp.hh:78
void set_digits(uint32_t digits)
Sets the number of output digits (supported range 1-10).
Definition totp.hh:90
const secure_string & secret() const
Returns the Base32 shared secret.
Definition totp.hh:75
void set_algorithm(Algorithm algorithm)
Sets the HMAC algorithm.
Definition totp.hh:96
Algorithm algorithm() const
Returns the HMAC algorithm.
Definition totp.hh:93
static std::optional< TotpConfig > Parse(const std::string &seed_field, const std::string &settings_field={})
Parses a TOTP seed/URI plus optional settings string.
uint32_t period() const
Returns the time step in seconds.
Definition totp.hh:81
Algorithm
Hash algorithms used by the HMAC-based TOTP construction.
Definition totp.hh:53
uint32_t digits() const
Returns the number of output digits.
Definition totp.hh:87
bool empty() const
Returns whether a secret is present.
Definition totp.hh:99
TOTP code generation (RFC 6238).
Definition totp.hh:119
static std::string Generate(const Entry &entry, std::time_t time=0)
Generates a code for the TOTP fields of an entry.
static std::string Generate(const TotpConfig &config, std::time_t time=0)
Generates the current TOTP code.
static uint32_t SecondsRemaining(const TotpConfig &config, std::time_t time=0)
Returns the number of seconds until the current time step expires.
String that is stored in wiped and best-effort locked memory.
Definition secure.hh:262
bool empty() const noexcept
Returns whether the string is empty.
Definition secure.hh:325
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Secure memory handling: zeroization, locked allocations and sensitive value containers.