From 798041fe11adb7fc24f5ca413e7e88844da2b750 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Sat, 14 Jan 2017 13:25:30 -0500 Subject: [PATCH] Extract readKeyFromLine. --- src/keys/CompositeKey.cpp | 28 +++++++++++++++++++++++++++- src/keys/CompositeKey.h | 2 ++ tests/TestKeys.cpp | 16 ++++++++++++++++ tests/TestKeys.h | 1 + utils/kdbx-extract.cpp | 20 ++++++-------------- utils/kdbx-merge.cpp | 29 ++--------------------------- 6 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/keys/CompositeKey.cpp b/src/keys/CompositeKey.cpp index 16b48592e..88116c104 100644 --- a/src/keys/CompositeKey.cpp +++ b/src/keys/CompositeKey.cpp @@ -18,12 +18,15 @@ #include "CompositeKey.h" #include "CompositeKey_p.h" -#include #include +#include +#include #include "core/Global.h" #include "crypto/CryptoHash.h" #include "crypto/SymmetricCipher.h" +#include "keys/FileKey.h" +#include "keys/PasswordKey.h" CompositeKey::CompositeKey() { @@ -71,6 +74,29 @@ CompositeKey& CompositeKey::operator=(const CompositeKey& key) return *this; } +/* + * Read a key from a line of input. + * If the line references a valid file + * path, the key is loaded from file. + */ +CompositeKey CompositeKey::readFromLine(QString line) +{ + + CompositeKey key; + if (QFile::exists(line)) { + FileKey fileKey; + fileKey.load(line); + key.addKey(fileKey); + } + else { + PasswordKey password; + password.setPassword(line); + key.addKey(password); + } + return key; + +} + QByteArray CompositeKey::rawKey() const { CryptoHash cryptoHash(CryptoHash::Sha256); diff --git a/src/keys/CompositeKey.h b/src/keys/CompositeKey.h index 3290d3671..f8666aadc 100644 --- a/src/keys/CompositeKey.h +++ b/src/keys/CompositeKey.h @@ -19,6 +19,7 @@ #define KEEPASSX_COMPOSITEKEY_H #include +#include #include "keys/Key.h" @@ -39,6 +40,7 @@ public: void addKey(const Key& key); static int transformKeyBenchmark(int msec); + static CompositeKey readFromLine(QString line); private: static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, diff --git a/tests/TestKeys.cpp b/tests/TestKeys.cpp index 6c1953faf..d5b35b1fb 100644 --- a/tests/TestKeys.cpp +++ b/tests/TestKeys.cpp @@ -83,6 +83,22 @@ void TestKeys::testComposite() delete compositeKey4; } +void TestKeys::testCompositeKeyReadFromLine() +{ + + QString keyFilename = QString("%1/FileKeyXml.key").arg(QString(KEEPASSX_TEST_DATA_DIR)); + + CompositeKey compositeFileKey = CompositeKey::readFromLine(keyFilename); + FileKey fileKey; + fileKey.load(keyFilename); + QCOMPARE(compositeFileKey.rawKey().size(), fileKey.rawKey().size()); + + CompositeKey compositePasswordKey = CompositeKey::readFromLine(QString("password")); + PasswordKey passwordKey(QString("password")); + QCOMPARE(compositePasswordKey.rawKey().size(), passwordKey.rawKey().size()); + +} + void TestKeys::testFileKey() { QFETCH(QString, type); diff --git a/tests/TestKeys.h b/tests/TestKeys.h index 0f14117fd..a6d0b7e1a 100644 --- a/tests/TestKeys.h +++ b/tests/TestKeys.h @@ -27,6 +27,7 @@ class TestKeys : public QObject private Q_SLOTS: void initTestCase(); void testComposite(); + void testCompositeKeyReadFromLine(); void testFileKey(); void testFileKey_data(); void testCreateFileKey(); diff --git a/utils/kdbx-extract.cpp b/utils/kdbx-extract.cpp index f5d2a19a6..6116b0365 100644 --- a/utils/kdbx-extract.cpp +++ b/utils/kdbx-extract.cpp @@ -33,8 +33,8 @@ int main(int argc, char **argv) { QCoreApplication app(argc, argv); - if (app.arguments().size() != 3) { - qCritical("Usage: kdbx-extract "); + if (app.arguments().size() != 2) { + qCritical("Usage: kdbx-extract "); return 1; } @@ -42,19 +42,11 @@ int main(int argc, char **argv) qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); } - CompositeKey key; - if (QFile::exists(app.arguments().at(1))) { - FileKey fileKey; - fileKey.load(app.arguments().at(1)); - key.addKey(fileKey); - } - else { - PasswordKey password; - password.setPassword(app.arguments().at(1)); - key.addKey(password); - } + static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); + QString line = inputTextStream.readLine(); + CompositeKey key = CompositeKey::readFromLine(line); - QFile dbFile(app.arguments().at(2)); + QFile dbFile(app.arguments().at(1)); if (!dbFile.exists()) { qCritical("File does not exist."); return 1; diff --git a/utils/kdbx-merge.cpp b/utils/kdbx-merge.cpp index d67a87672..da780ea1b 100644 --- a/utils/kdbx-merge.cpp +++ b/utils/kdbx-merge.cpp @@ -29,31 +29,6 @@ #include "format/KeePass2Reader.h" #include "format/KeePass2Writer.h" #include "keys/CompositeKey.h" -#include "keys/FileKey.h" -#include "keys/PasswordKey.h" - -/* - * Read a key from a line of input. - * If the line references a valid file - * path, the key is loaded from file. - */ -CompositeKey readKeyFromLine(QString line) -{ - - CompositeKey key; - if (QFile::exists(line)) { - FileKey fileKey; - fileKey.load(line); - key.addKey(fileKey); - } - else { - PasswordKey password; - password.setPassword(line); - key.addKey(password); - } - return key; - -} int main(int argc, char **argv) { @@ -85,7 +60,7 @@ int main(int argc, char **argv) static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); QString line1 = inputTextStream.readLine(); - CompositeKey key1 = readKeyFromLine(line1); + CompositeKey key1 = CompositeKey::readFromLine(line1); CompositeKey key2; if (parser.isSet("same-password")) { @@ -93,7 +68,7 @@ int main(int argc, char **argv) } else { QString line2 = inputTextStream.readLine(); - key2 = readKeyFromLine(line2); + key2 = CompositeKey::readFromLine(line2); }