QCA

keyloader.cpp
1/*
2 Copyright (C) 2007 Justin Karneges <justin@affinix.com>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21
22// QtCrypto has the declarations for all of QCA
23#include <QtCrypto>
24
25#include <QCoreApplication>
26#include <QFile>
27#include <QTimer>
28
29#include <cstdio>
30
31#ifdef QT_STATICPLUGIN
32#include "import_plugins.h"
33#endif
34
35class PassphraseHandler : public QObject
36{
38public:
39 QCA::EventHandler handler;
40
41 PassphraseHandler(QObject *parent = nullptr)
43 {
44 connect(&handler, &QCA::EventHandler::eventReady, this, &PassphraseHandler::eh_eventReady);
45 handler.start();
46 }
47
48private Q_SLOTS:
49 void eh_eventReady(int id, const QCA::Event &event)
50 {
51 if (event.type() == QCA::Event::Password) {
53 QCA::ConsolePrompt prompt;
54 prompt.getHidden(QStringLiteral("Passphrase"));
55 prompt.waitForFinished();
56 pass = prompt.result();
57 handler.submitPassword(id, pass);
58 } else
59 handler.reject(id);
60 }
61};
62
63class App : public QObject
64{
66public:
67 QCA::KeyLoader keyLoader;
68 QString str;
69
70 App()
71 {
72 connect(&keyLoader, &QCA::KeyLoader::finished, this, &App::kl_finished);
73 }
74
75public Q_SLOTS:
76 void start()
77 {
78 keyLoader.loadPrivateKeyFromPEMFile(str);
79 }
80
82 void quit();
83
84private Q_SLOTS:
85 void kl_finished()
86 {
87 if (keyLoader.convertResult() == QCA::ConvertGood) {
88 QCA::PrivateKey key = keyLoader.privateKey();
89 printf("Loaded successfully. Bits: %d\n", key.bitSize());
90 } else
91 printf("Unable to load.\n");
92
93 emit quit();
94 }
95};
96
97int main(int argc, char **argv)
98{
100 QCoreApplication qapp(argc, argv);
101
102 if (argc < 2) {
103 printf("usage: keyloader [privatekey.pem]\n");
104 return 0;
105 }
106
107 PassphraseHandler passphraseHandler;
108 App app;
109 app.str = QFile::decodeName(argv[1]);
110 QObject::connect(&app, &App::quit, &qapp, QCoreApplication::quit);
111 QTimer::singleShot(0, &app, &App::start);
112 qapp.exec();
113 return 0;
114}
115
116#include "keyloader.moc"
Console prompt handler.
void waitForFinished()
Block waiting for user input.
void getHidden(const QString &promptStr)
Allow the user to enter data without it being echo'd to the terminal.
SecureArray result() const
Obtain the result of the user input.
Interface class for password / passphrase / PIN and token handlers.
Definition qca_core.h:1579
void submitPassword(int id, const SecureArray &password)
function to call to return the user provided password, passphrase or PIN.
void eventReady(int id, const QCA::Event &context)
signal emitted when an Event requires attention.
void reject(int id)
function to call to indicate that the user declined to provide a password, passphrase,...
void start()
mandatory function to call after connecting the signal to a slot in your application specific passwor...
An asynchronous event.
Definition qca_core.h:1391
@ Password
Asking for a password, PIN or passphrase.
Definition qca_core.h:1400
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Asynchronous private key loader.
Definition qca_cert.h:2613
ConvertResult convertResult() const
The result of the loading process.
void loadPrivateKeyFromPEMFile(const QString &fileName)
Initiate an asynchronous loading of a PrivateKey from a PEM format file.
PrivateKey privateKey() const
The private key that has been loaded.
void finished()
Signal that is emitted when the load process has completed.
int bitSize() const
Report the number of bits in the key.
Generic private key.
Secure array of bytes.
Definition qca_tools.h:317
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
@ ConvertGood
Conversion succeeded, results should be valid.
QString decodeName(const QByteArray &localFileName)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:48:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.