The code below shows how to load a private key from a PEM format file, including handling any requirement for a passphrase. This is done using the QCA::KeyLoader class.
#include <QtCrypto>
#include <QCoreApplication>
#include <QFile>
#include <QTimer>
#include <cstdio>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
class PassphraseHandler :
public QObject
{
Q_OBJECT
public:
PassphraseHandler(
QObject *parent =
nullptr)
{
}
private Q_SLOTS:
void eh_eventReady(
int id,
const QCA::Event &event)
{
prompt.
getHidden(QStringLiteral(
"Passphrase"));
} else
}
};
{
Q_OBJECT
public:
QString str;
App()
{
}
public Q_SLOTS:
void start()
{
}
Q_SIGNALS:
void quit();
private Q_SLOTS:
void kl_finished()
{
printf(
"Loaded successfully. Bits: %d\n", key.
bitSize());
} else
printf("Unable to load.\n");
emit quit();
}
};
int main(int argc, char **argv)
{
QCoreApplication qapp(argc, argv);
if (argc < 2) {
printf("usage: keyloader [privatekey.pem]\n");
return 0;
}
PassphraseHandler passphraseHandler;
App app;
app.str = QFile::decodeName(argv[1]);
QObject::connect(&app, &App::quit, &qapp, QCoreApplication::quit);
QTimer::singleShot(0, &app, &App::start);
qapp.exec();
return 0;
}
#include "keyloader.moc"
Console prompt handler.
Definition: qca_support.h:864
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
Type type() const
the Type of this event
@ 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.
Definition: qca_publickey.h:833
Secure array of bytes.
Definition: qca_tools.h:317
QCA_EXPORT void init()
Initialise QCA.
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:120