From 0ad4b3d8fea1238facf14c55fa30215ae82c7c94 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 25 Apr 2012 18:35:30 +0200 Subject: [PATCH] Minor improvements in CompositeKey and TestKeys. --- src/keys/CompositeKey.cpp | 14 ++++++++------ src/keys/CompositeKey.h | 3 ++- tests/TestKeys.cpp | 13 +++++++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/keys/CompositeKey.cpp b/src/keys/CompositeKey.cpp index 30077e55e..f2236aa13 100644 --- a/src/keys/CompositeKey.cpp +++ b/src/keys/CompositeKey.cpp @@ -33,7 +33,7 @@ CompositeKey::CompositeKey(const CompositeKey& key) CompositeKey::~CompositeKey() { - qDeleteAll(m_keys); + clear(); } void CompositeKey::clear() @@ -51,8 +51,8 @@ CompositeKey& CompositeKey::operator=(const CompositeKey& key) { clear(); - Q_FOREACH (Key* subKey, key.m_keys) { - m_keys.append(subKey->clone()); + Q_FOREACH (const Key* subKey, key.m_keys) { + addKey(*subKey); } return *this; @@ -62,7 +62,7 @@ QByteArray CompositeKey::rawKey() const { CryptoHash cryptoHash(CryptoHash::Sha256); - Q_FOREACH (Key* key, m_keys) { + Q_FOREACH (const Key* key, m_keys) { cryptoHash.addData(key->rawKey()); } @@ -86,9 +86,11 @@ QByteArray CompositeKey::transform(const QByteArray& seed, int rounds) const return CryptoHash::hash(transformed, CryptoHash::Sha256); } -QByteArray CompositeKey::transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds) { +QByteArray CompositeKey::transformKeyRaw(const QByteArray& key, const QByteArray& seed, + int rounds) { QByteArray iv(16, 0); - SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, SymmetricCipher::Encrypt, seed, iv); + SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, + SymmetricCipher::Encrypt, seed, iv); QByteArray result = key; diff --git a/src/keys/CompositeKey.h b/src/keys/CompositeKey.h index 6cd86ab8d..3fa438681 100644 --- a/src/keys/CompositeKey.h +++ b/src/keys/CompositeKey.h @@ -37,7 +37,8 @@ public: void addKey(const Key& key); private: - static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds); + static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, + int rounds); QList m_keys; }; diff --git a/tests/TestKeys.cpp b/tests/TestKeys.cpp index 116de3632..38bb0707d 100644 --- a/tests/TestKeys.cpp +++ b/tests/TestKeys.cpp @@ -42,27 +42,32 @@ void TestKeys::testComposite() PasswordKey* passwordKey1 = new PasswordKey(); PasswordKey* passwordKey2 = new PasswordKey("test"); + // make sure that addKey() creates a copy of the keys compositeKey1->addKey(*passwordKey1); compositeKey1->addKey(*passwordKey2); delete passwordKey1; delete passwordKey2; - QVERIFY(compositeKey1->transform(QByteArray(32, '\0'), 1).size() == 32); + QByteArray transformed = compositeKey1->transform(QByteArray(32, '\0'), 1); + QCOMPARE(transformed.size(), 32); // make sure the subkeys are copied CompositeKey* compositeKey2 = compositeKey1->clone(); delete compositeKey1; - - QVERIFY(compositeKey2->transform(QByteArray(32, '\0'), 1).size() == 32); - + QCOMPARE(compositeKey2->transform(QByteArray(32, '\0'), 1), transformed); delete compositeKey2; CompositeKey* compositeKey3 = new CompositeKey(); CompositeKey* compositeKey4 = new CompositeKey(); + // test clear() compositeKey3->addKey(PasswordKey("test")); compositeKey3->clear(); + QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey()); + // test assignment operator + compositeKey3->addKey(PasswordKey("123")); + *compositeKey4 = *compositeKey3; QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey()); delete compositeKey3;