Commit fcf0eddb authored by Andrea Scarpino's avatar Andrea Scarpino

Support all algorithm versions

parent 212a43c9
......@@ -52,6 +52,19 @@ Page {
echoMode: TextInput.Password
}
ComboBox {
id: version
label: qsTr("Algorithm version")
currentIndex: 3
menu: ContextMenu {
MenuItem { text: qsTr("V0") }
MenuItem { text: qsTr("V1") }
MenuItem { text: qsTr("V2") }
MenuItem { text: qsTr("V3") }
}
}
Button {
id: save
text: qsTr("Save");
......@@ -59,7 +72,7 @@ Page {
onClicked: {
if (name.text.length > 0 && password.text.length > 0) {
manager.setUserData(name.text, password.text);
manager.generateMasterKey(name.text, password.text, version.currentIndex);
pageStack.pop();
}
}
......
......@@ -26,11 +26,6 @@
#include <QDebug>
extern "C"
{
#include <mpw-algorithm.h>
}
MPWManager::MPWManager(QObject *parent) :
QObject(parent)
{
......@@ -40,28 +35,72 @@ MPWManager::~MPWManager()
{
}
MPWManager::AlgorithmVersion MPWManager::getAlgorithmVersion() const
{
return m_algVersion;
}
QString MPWManager::getName() const
{
return m_name;
}
void MPWManager::setUserData(const QString &name, const QString &password)
void MPWManager::setAlgorithmVersion(AlgorithmVersion version)
{
m_algVersion = version;
}
void MPWManager::setName(const QString &name)
{
m_name = name;
}
void MPWManager::generateMasterKey(const QString &name, const QString &password, AlgorithmVersion version)
{
setName(name);
setAlgorithmVersion(version);
qDebug() << "Using algorithm version:" << m_algVersion;
const uint8_t* k = mpw_masterKeyForUser(name.toUtf8().data(), password.toUtf8().data(),
MPAlgorithmVersionCurrent);
const uint8_t* k = mpw_masterKeyForUser(name.toUtf8().data(), password.toUtf8().data(), toMPAlgorithmVersion(version));
if (k) {
m_key = QByteArray::fromRawData((const char*) k, MP_dkLen);
} else {
qCritical() << "Error during master key generation";
qCritical() << "Error during master key generation.";
}
}
MPAlgorithmVersion MPWManager::toMPAlgorithmVersion(AlgorithmVersion version) const
{
MPAlgorithmVersion v = MPAlgorithmVersionCurrent;
switch (version) {
case V0: v = MPAlgorithmVersion0; break;
case V1: v = MPAlgorithmVersion1; break;
case V2: v = MPAlgorithmVersion2; break;
case V3: v = MPAlgorithmVersion3; break;
default: qCritical() << "Unrecognized algorithm version:" << version;
}
return v;
}
QString MPWManager::getPassword(const QString &site, PasswordType type, const uint counter) const
{
MPSiteType t = 0;
const char* p = mpw_passwordForSite((const unsigned char*) m_key.data(), site.toUtf8().data(),
toMPSiteType(type), counter, MPSiteVariantPassword, NULL,
toMPAlgorithmVersion(m_algVersion));
if (p) {
return QString::fromUtf8(p);
} else {
qCritical() << "Error during password generation.";
return QString();
}
}
MPSiteType MPWManager::toMPSiteType(PasswordType type) const
{
MPSiteType t = MPSiteTypeGeneratedLong;
switch (type) {
case Maximum: t = MPSiteTypeGeneratedMaximum; break;
case Long: t = MPSiteTypeGeneratedLong; break;
......@@ -71,16 +110,8 @@ QString MPWManager::getPassword(const QString &site, PasswordType type, const ui
case PIN: t = MPSiteTypeGeneratedPIN; break;
case Name: t = MPSiteTypeGeneratedName; break;
case Phrase: t = MPSiteTypeGeneratedPhrase; break;
default: qCritical() << "Unrecognized password type" << type;
default: qCritical() << "Unrecognized password type:" << type;
}
const char* p = mpw_passwordForSite((const unsigned char*) m_key.data(), site.toUtf8().data(),
t, counter, MPSiteVariantPassword, NULL, MPAlgorithmVersionCurrent);
if (p) {
return QString::fromUtf8(p);
} else {
qCritical() << "Error during password generation";
return QString();
}
return t;
}
......@@ -27,6 +27,11 @@
#include <QObject>
extern "C"
{
#include <mpw-algorithm.h>
}
class MPWManager : public QObject
{
Q_OBJECT
......@@ -36,16 +41,29 @@ public:
};
Q_ENUMS(PasswordType)
enum AlgorithmVersion {
V0, V1, V2, V3
};
Q_ENUMS(AlgorithmVersion)
explicit MPWManager(QObject *parent = 0);
virtual ~MPWManager();
Q_INVOKABLE AlgorithmVersion getAlgorithmVersion() const;
Q_INVOKABLE QString getName() const;
Q_INVOKABLE void setUserData(const QString &name, const QString &password);
Q_INVOKABLE void setAlgorithmVersion(AlgorithmVersion version);
Q_INVOKABLE void setName(const QString &name);
Q_INVOKABLE void generateMasterKey(const QString &name, const QString &password, AlgorithmVersion version);
Q_INVOKABLE QString getPassword(const QString &site, PasswordType type, const uint counter) const;
private:
MPAlgorithmVersion toMPAlgorithmVersion(AlgorithmVersion version) const;
MPSiteType toMPSiteType(PasswordType type) const;
QByteArray m_key;
QString m_name;
AlgorithmVersion m_algVersion;
};
#endif // MPWMANAGER_H
<?xml version="1.0" ?><!DOCTYPE TS><TS language="it" version="2.1">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="it">
<context>
<name>MainPage</name>
<message>
......@@ -96,8 +98,33 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<source>Save</source>
<translation>Salva</translation>
</message>
</context>
</TS>
\ No newline at end of file
</TS>
<?xml version="1.0" ?><!DOCTYPE TS><TS language="sv" version="2.1">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="sv">
<context>
<name>MainPage</name>
<message>
......@@ -96,8 +98,33 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<source>Save</source>
<translation>Spara</translation>
</message>
</context>
</TS>
\ No newline at end of file
</TS>
......@@ -98,6 +98,31 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<source>Save</source>
<translation type="unfinished"></translation>
</message>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment