Minor improvements in CompositeKey and TestKeys.

This commit is contained in:
Felix Geyer 2012-04-25 18:35:30 +02:00
parent f82de78861
commit 0ad4b3d8fe
3 changed files with 19 additions and 11 deletions

View File

@ -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;

View File

@ -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<Key*> m_keys;
};

View File

@ -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;