1 /*
2  *  Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
3  *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 2 or (at your option)
8  *  version 3 of the License.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef QTOTP_H
20 #define QTOTP_H
21 
22 #include <QMap>
23 #include <QString>
24 #include <QtCore/QSharedPointer>
25 #include <QtCore/qglobal.h>
26 
27 class QUrl;
28 
29 namespace Totp
30 {
31     struct Encoder
32     {
33         QString name;
34         QString shortName;
35         QString alphabet;
36         uint digits;
37         uint step;
38         bool reverse;
39     };
40 
41     enum Algorithm
42     {
43         Sha1,
44         Sha256,
45         Sha512,
46     };
47 
48     enum StorageFormat
49     {
50         OTPURL,
51         KEEOTP,
52         LEGACY,
53     };
54 
55     struct Settings
56     {
57         Totp::StorageFormat format;
58         Totp::Encoder encoder;
59         Totp::Algorithm algorithm;
60         QString key;
61         bool custom;
62         uint digits;
63         uint step;
64     };
65 
66     constexpr uint DEFAULT_STEP = 30u;
67     constexpr uint DEFAULT_DIGITS = 6u;
68     constexpr uint STEAM_DIGITS = 5u;
69     constexpr Totp::Algorithm DEFAULT_ALGORITHM = Sha1;
70     constexpr Totp::StorageFormat DEFAULT_FORMAT = OTPURL;
71     static const QString STEAM_SHORTNAME = "S";
72 
73     static const QString ATTRIBUTE_OTP = "otp";
74     static const QString ATTRIBUTE_SEED = "TOTP Seed";
75     static const QString ATTRIBUTE_SETTINGS = "TOTP Settings";
76 
77     QSharedPointer<Totp::Settings> parseSettings(const QString& rawSettings, const QString& key = {});
78     QSharedPointer<Totp::Settings> createSettings(const QString& key,
79                                                   const uint digits,
80                                                   const uint step,
81                                                   const Totp::StorageFormat format = DEFAULT_FORMAT,
82                                                   const QString& encoderShortName = {},
83                                                   const Totp::Algorithm algorithm = DEFAULT_ALGORITHM);
84     QString writeSettings(const QSharedPointer<Totp::Settings>& settings,
85                           const QString& title = {},
86                           const QString& username = {},
87                           bool forceOtp = false);
88 
89     QString generateTotp(const QSharedPointer<Totp::Settings>& settings, const quint64 time = 0ull);
90 
91     QList<QPair<QString, QString>> supportedEncoders();
92     QList<QPair<QString, Algorithm>> supportedAlgorithms();
93 
94     Encoder& defaultEncoder();
95     Encoder& steamEncoder();
96     Encoder& getEncoderByShortName(const QString& shortName);
97     Encoder& getEncoderByName(const QString& name);
98 } // namespace Totp
99 
100 #endif // QTOTP_H
101