Ejemplo n.º 1
0
void TestKeePass1Reader::reopenDatabase(Database* db, const QString& password, const QString& keyfileName)
{
    QBuffer buffer;
    buffer.open(QIODevice::ReadWrite);

    KeePass2Writer writer;
    writer.writeDatabase(&buffer, db);
    QVERIFY(!writer.hasError());
    QVERIFY(buffer.seek(0));

    CompositeKey key;
    if (!password.isNull()) {
        key.addKey(PasswordKey(password));
    }
    if (!keyfileName.isEmpty()) {
        FileKey fileKey;
        QVERIFY(fileKey.load(keyfileName));
        key.addKey(fileKey);
    }

    KeePass2Reader reader;
    Database* newDb = reader.readDatabase(&buffer, key);
    QVERIFY(newDb);
    QVERIFY(!reader.hasError());
    delete newDb;
}
Ejemplo n.º 2
0
bool Database::writeDatabase(QIODevice* device, QString* error)
{
    Q_ASSERT(!m_data.isReadOnly);
    if (m_data.isReadOnly) {
        if (error) {
            *error = tr("File cannot be written as it is opened in read-only mode.");
        }
        return false;
    }

    QByteArray oldTransformedKey = m_data.transformedMasterKey;
    KeePass2Writer writer;
    setEmitModified(false);
    writer.writeDatabase(device, this);
    setEmitModified(true);

    if (writer.hasError()) {
        // the writer failed
        if (error) {
            *error = writer.errorString();
        }
        return false;
    }

    Q_ASSERT(!m_data.transformedMasterKey.isEmpty());
    Q_ASSERT(m_data.transformedMasterKey != oldTransformedKey);
    if (m_data.transformedMasterKey.isEmpty() || m_data.transformedMasterKey == oldTransformedKey) {
        if (error) {
            *error = tr("Key not transformed. This is a bug, please report it to the developers!");
        }
        return false;
    }

    markAsClean();
    return true;
}