4
0
mirror of https://github.com/QuasarApp/qca.git synced 2025-05-10 09:49:33 +00:00

move the console password prompter out of qcatool and into qca

svn path=/trunk/kdesupport/qca/; revision=604538
This commit is contained in:
Justin Karneges 2006-11-13 07:10:37 +00:00
parent 12c1b9bcdf
commit f7933603e2
3 changed files with 145 additions and 5 deletions
include/QtCrypto
src/support
tools/qcatool

@ -25,7 +25,7 @@
Header file for "support" classes used in %QCA
The classes in this header do not have any cryptographic
content - they are used in %QCA, and are included for convenience.
content - they are used in %QCA, and are included for convenience.
\note You should not use this header directly from an
application. You should just use <tt> \#include \<QtCrypto>
@ -235,6 +235,21 @@ namespace QCA
friend class Console;
};
class ConsolePrompt : public QObject
{
Q_OBJECT
public:
static QSecureArray getHidden(const QString &promptStr);
static void waitForEnter();
private:
class Private;
Private *d;
ConsolePrompt(QObject *parent = 0);
~ConsolePrompt();
};
}
#endif

@ -570,6 +570,131 @@ int ConsoleReference::bytesToWrite() const
return d->thread->bytesToWrite();
}
//----------------------------------------------------------------------------
// ConsolePrompt
//----------------------------------------------------------------------------
class ConsolePrompt::Private : public QObject
{
Q_OBJECT
public:
Synchronizer sync;
ConsoleReference console;
QSecureArray result;
int at;
bool done;
bool enter;
Private() : sync(this), console(this)
{
connect(&console, SIGNAL(readyRead()), SLOT(con_readyRead()));
connect(&console, SIGNAL(closed()), SLOT(con_closed()));
}
bool start(bool enterMode)
{
result.clear();
at = 0;
done = false;
enter = enterMode;
if(!console.start(QCA::ConsoleReference::SecurityEnabled))
{
printf("Console input not available or closed\n");
return false;
}
sync.waitForCondition();
return true;
}
bool processChar(unsigned char c)
{
if(c == '\r' || c == '\n')
{
printf("\n");
if(!done)
{
sync.conditionMet();
done = true;
}
return false;
}
if(enter)
return true;
if(c == '\b' || c == 0x7f)
{
if(at > 0)
{
--at;
printf("\b \b");
fflush(stdout);
result.resize(at);
}
return true;
}
else if(c < 0x20)
return true;
if(at + 1 > result.size())
result.resize(at + 1);
result[at++] = c;
printf("*");
fflush(stdout);
return true;
}
private slots:
void con_readyRead()
{
while(console.bytesAvailable() > 0)
{
QSecureArray buf = console.readSecure(1);
if(buf.isEmpty())
continue;
if(!processChar(buf[0]))
break;
}
}
void con_closed()
{
printf("Console closed\n");
if(!done)
{
sync.conditionMet();
done = true;
}
}
};
ConsolePrompt::ConsolePrompt(QObject *parent)
:QObject(parent)
{
d = new Private;
}
ConsolePrompt::~ConsolePrompt()
{
delete d;
}
QSecureArray ConsolePrompt::getHidden(const QString &promptStr)
{
printf("%s: ", qPrintable(promptStr));
fflush(stdout);
ConsolePrompt p;
if(!p.d->start(false))
return QSecureArray();
return p.d->result;
}
void ConsolePrompt::waitForEnter()
{
ConsolePrompt p;
p.d->start(true);
}
}
#include "console.moc"

@ -155,7 +155,7 @@ private slots:
}
};
class Prompter : public QObject
/*class Prompter : public QObject
{
Q_OBJECT
public:
@ -257,7 +257,7 @@ private slots:
{
printf("Console closed\n");
}
};
};*/
class PassphrasePrompt : public QObject
{
@ -313,7 +313,7 @@ private slots:
else
str = QString("Enter %1").arg(type);
QSecureArray result = Prompter::prompt(str);
QSecureArray result = QCA::ConsolePrompt::getHidden(str);
handler.submitPassword(id, result);
}
else if(e.type() == QCA::Event::Token)
@ -321,7 +321,7 @@ private slots:
//QCA::KeyStoreEntry entry(e.keyStoreEntryId());
//printf("Please insert token for [%s] and press Enter ...\n", qPrintable(entry.name()));
printf("Please insert token and press Enter ...\n");
Prompter::waitForEnter();
QCA::ConsolePrompt::waitForEnter();
handler.tokenOkay(id);
}
}