());
cursor.moveToFirst();
while ( ! cursor.isAfterLast() ) {
String sUUID = cursor.getString(0);
UUID uuid = UUID.fromString(sUUID);
Log.d("TAG", uuid.toString());
- PwEntryV3 entry = db.entries.get(uuid).get();
+ PwEntryV3 entry = (PwEntryV3) db.entries.get(uuid).get();
group.childEntries.add(entry);
cursor.moveToNext();
diff --git a/src/com/keepassdroid/settings/AppSettingsActivity.java b/src/com/keepassdroid/settings/AppSettingsActivity.java
index f3cc87c18..167813641 100644
--- a/src/com/keepassdroid/settings/AppSettingsActivity.java
+++ b/src/com/keepassdroid/settings/AppSettingsActivity.java
@@ -84,7 +84,7 @@ public class AppSettingsActivity extends LockingClosePreferenceActivity {
}
private void setRounds(Database db, Preference rounds) {
- rounds.setSummary(Integer.toString(db.pm.numKeyEncRounds));
+ rounds.setSummary(Long.toString(db.pm.getNumRounds()));
}
diff --git a/src/com/keepassdroid/settings/RoundsPreference.java b/src/com/keepassdroid/settings/RoundsPreference.java
index 8f937fa8f..d923397c5 100644
--- a/src/com/keepassdroid/settings/RoundsPreference.java
+++ b/src/com/keepassdroid/settings/RoundsPreference.java
@@ -32,13 +32,13 @@ import com.android.keepass.R;
import com.keepassdroid.Database;
import com.keepassdroid.ProgressTask;
import com.keepassdroid.app.App;
-import com.keepassdroid.database.PwDatabaseV3;
+import com.keepassdroid.database.PwDatabase;
import com.keepassdroid.database.edit.OnFinish;
import com.keepassdroid.database.edit.SaveDB;
public class RoundsPreference extends DialogPreference {
- private PwDatabaseV3 mPM;
+ private PwDatabase mPM;
private TextView mRoundsView;
@Override
@@ -49,8 +49,8 @@ public class RoundsPreference extends DialogPreference {
Database db = App.getDB();
mPM = db.pm;
- int numRounds = mPM.numKeyEncRounds;
- mRoundsView.setText(Integer.toString(numRounds));
+ long numRounds = mPM.getNumRounds();
+ mRoundsView.setText(Long.toString(numRounds));
return view;
}
@@ -82,8 +82,13 @@ public class RoundsPreference extends DialogPreference {
rounds = 1;
}
- int oldRounds = mPM.numKeyEncRounds;
- mPM.numKeyEncRounds = rounds;
+ long oldRounds = mPM.getNumRounds();
+ try {
+ mPM.setNumRonuds(rounds);
+ } catch (NumberFormatException e) {
+ Toast.makeText(getContext(), R.string.error_rounds_too_large, Toast.LENGTH_LONG).show();
+ mPM.setNumRonuds(Integer.MAX_VALUE);
+ }
Handler handler = new Handler();
SaveDB save = new SaveDB(App.getDB(), new AfterSave(getContext(), handler, oldRounds));
@@ -95,10 +100,10 @@ public class RoundsPreference extends DialogPreference {
}
private class AfterSave extends OnFinish {
- private int mOldRounds;
+ private long mOldRounds;
private Context mCtx;
- public AfterSave(Context ctx, Handler handler, int oldRounds) {
+ public AfterSave(Context ctx, Handler handler, long oldRounds) {
super(handler);
mCtx = ctx;
@@ -114,7 +119,7 @@ public class RoundsPreference extends DialogPreference {
}
} else {
displayMessage(mCtx);
- mPM.numKeyEncRounds = mOldRounds;
+ mPM.setNumRonuds(mOldRounds);
}
super.run();
diff --git a/src/com/keepassdroid/stream/BetterCipherInputStream.java b/src/com/keepassdroid/stream/BetterCipherInputStream.java
new file mode 100644
index 000000000..b91964003
--- /dev/null
+++ b/src/com/keepassdroid/stream/BetterCipherInputStream.java
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.keepassdroid.stream;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.GeneralSecurityException;
+
+import javax.crypto.Cipher;
+import javax.crypto.NullCipher;
+
+/**
+ * This class wraps an {@code InputStream} and a cipher so that {@code read()}
+ * methods return data that are read from the underlying {@code InputStream} and
+ * processed by the cipher.
+ *
+ * The cipher must be initialized for the requested operation before being used
+ * by a {@code BetterCipherInputStream}. For example, if a cipher initialized for
+ * decryption is used with a {@code BetterCipherInputStream}, the {@code
+ * BetterCipherInputStream} tries to read the data an decrypt them before returning.
+ */
+public class BetterCipherInputStream extends FilterInputStream {
+
+ private final Cipher cipher;
+ private static final int I_DEFAULT_BUFFER_SIZE = 8 * 1024;
+ private final byte[] i_buffer;
+ private int index; // index of the bytes to return from o_buffer
+ private byte[] o_buffer;
+ private boolean finished;
+
+ /**
+ * Creates a new {@code BetterCipherInputStream} instance for an {@code
+ * InputStream} and a cipher.
+ *
+ * @param is
+ * the input stream to read data from.
+ * @param c
+ * the cipher to process the data with.
+ */
+ public BetterCipherInputStream(InputStream is, Cipher c) {
+ this(is, c, I_DEFAULT_BUFFER_SIZE);
+ }
+
+ /**
+ * Creates a new {@code BetterCipherInputStream} instance for an {@code
+ * InputStream} and a cipher.
+ *
+ * @param is
+ * the input stream to read data from.
+ * @param c
+ * the cipher to process the data with.
+ * @param bufferSize
+ * size to buffer output from the cipher
+ */
+ public BetterCipherInputStream(InputStream is, Cipher c, int bufferSize) {
+ super(is);
+ this.cipher = c;
+ i_buffer = new byte[bufferSize];
+ }
+
+ /**
+ * Creates a new {@code BetterCipherInputStream} instance for an {@code
+ * InputStream} without a cipher.
+ *
+ * A {@code NullCipher} is created and used to process the data.
+ *
+ * @param is
+ * the input stream to read data from.
+ */
+ protected BetterCipherInputStream(InputStream is) {
+ this(is, new NullCipher());
+ }
+
+ /**
+ * Reads the next byte from this cipher input stream.
+ *
+ * @return the next byte, or {@code -1} if the end of the stream is reached.
+ * @throws IOException
+ * if an error occurs.
+ */
+ @Override
+ public int read() throws IOException {
+ if (finished) {
+ return ((o_buffer == null) || (index == o_buffer.length))
+ ? -1
+ : o_buffer[index++] & 0xFF;
+ }
+ if ((o_buffer != null) && (index < o_buffer.length)) {
+ return o_buffer[index++] & 0xFF;
+ }
+ index = 0;
+ o_buffer = null;
+ int num_read;
+ while (o_buffer == null) {
+ if ((num_read = in.read(i_buffer)) == -1) {
+ try {
+ o_buffer = cipher.doFinal();
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
+ finished = true;
+ break;
+ }
+ o_buffer = cipher.update(i_buffer, 0, num_read);
+ }
+ return read();
+ }
+
+ /**
+ * Reads the next {@code b.length} bytes from this input stream into buffer
+ * {@code b}.
+ *
+ * @param b
+ * the buffer to be filled with data.
+ * @return the number of bytes filled into buffer {@code b}, or {@code -1}
+ * if the end of the stream is reached.
+ * @throws IOException
+ * if an error occurs.
+ */
+ @Override
+ public int read(byte[] b) throws IOException {
+ return read(b, 0, b.length);
+ }
+
+ /**
+ * Reads the next {@code len} bytes from this input stream into buffer
+ * {@code b} starting at offset {@code off}.
+ *
+ * if {@code b} is {@code null}, the next {@code len} bytes are read and
+ * discarded.
+ *
+ * @param b
+ * the buffer to be filled with data.
+ * @param off
+ * the offset to start in the buffer.
+ * @param len
+ * the maximum number of bytes to read.
+ * @return the number of bytes filled into buffer {@code b}, or {@code -1}
+ * of the of the stream is reached.
+ * @throws IOException
+ * if an error occurs.
+ * @throws NullPointerException
+ * if the underlying input stream is {@code null}.
+ */
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (in == null) {
+ throw new NullPointerException("Underlying input stream is null");
+ }
+
+ int read_b;
+ int i;
+ for (i=0; i
+ * The number of bytes skipped depends on the result of a call to
+ * {@link BetterCipherInputStream#available() available}. The smaller of n and the
+ * result are the number of bytes being skipped.
+ *
+ * @param n
+ * the number of bytes that should be skipped.
+ * @return the number of bytes actually skipped.
+ * @throws IOException
+ * if an error occurs
+ */
+ @Override
+ public long skip(long n) throws IOException {
+ long i = 0;
+ int available = available();
+ if (available < n) {
+ n = available;
+ }
+ while ((i < n) && (read() != -1)) {
+ i++;
+ }
+ return i;
+ }
+
+ /**
+ * Returns the number of bytes available without blocking.
+ *
+ * @return the number of bytes available, currently zero.
+ * @throws IOException
+ * if an error occurs
+ */
+ @Override
+ public int available() throws IOException {
+ return 0;
+ }
+
+ /**
+ * Closes this {@code BetterCipherInputStream}, also closes the underlying input
+ * stream and call {@code doFinal} on the cipher object.
+ *
+ * @throws IOException
+ * if an error occurs.
+ */
+ @Override
+ public void close() throws IOException {
+ in.close();
+ try {
+ cipher.doFinal();
+ } catch (GeneralSecurityException ignore) {
+ //do like RI does
+ }
+
+ }
+
+ /**
+ * Returns whether this input stream supports {@code mark} and
+ * {@code reset}, which it does not.
+ *
+ * @return false, since this input stream does not support {@code mark} and
+ * {@code reset}.
+ */
+ @Override
+ public boolean markSupported() {
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/tests/src/com/keepassdroid/tests/PwEntryTest.java b/tests/src/com/keepassdroid/tests/PwEntryTest.java
index cca53657b..32a39a3d0 100644
--- a/tests/src/com/keepassdroid/tests/PwEntryTest.java
+++ b/tests/src/com/keepassdroid/tests/PwEntryTest.java
@@ -37,7 +37,7 @@ public class PwEntryTest extends AndroidTestCase {
protected void setUp() throws Exception {
super.setUp();
- mPE = TestData.GetTest1(getContext()).entries.get(0);
+ mPE = (PwEntryV3) TestData.GetTest1(getContext()).entries.get(0);
}
@@ -49,7 +49,7 @@ public class PwEntryTest extends AndroidTestCase {
String sPass = "12345";
byte[] password = sPass.getBytes("UTF-8");
- assertArrayEquals(password, mPE.getPassword());
+ assertArrayEquals(password, mPE.getPasswordBytes());
}
public void testCreation() {
diff --git a/tests/src/com/keepassdroid/tests/PwGroupTest.java b/tests/src/com/keepassdroid/tests/PwGroupTest.java
index e2777494c..7b4c519d6 100644
--- a/tests/src/com/keepassdroid/tests/PwGroupTest.java
+++ b/tests/src/com/keepassdroid/tests/PwGroupTest.java
@@ -33,7 +33,7 @@ public class PwGroupTest extends AndroidTestCase {
protected void setUp() throws Exception {
super.setUp();
- mPG = TestData.GetTest1(getContext()).groups.get(0);
+ mPG = (PwGroupV3) TestData.GetTest1(getContext()).getGroups().get(0);
}
diff --git a/tests/src/com/keepassdroid/tests/database/DeleteEntry.java b/tests/src/com/keepassdroid/tests/database/DeleteEntry.java
index 4fb305e20..b47223779 100644
--- a/tests/src/com/keepassdroid/tests/database/DeleteEntry.java
+++ b/tests/src/com/keepassdroid/tests/database/DeleteEntry.java
@@ -21,13 +21,13 @@ package com.keepassdroid.tests.database;
import java.util.Vector;
-
import android.content.Context;
import android.test.AndroidTestCase;
import com.keepassdroid.Database;
import com.keepassdroid.database.PwDatabaseV3;
-import com.keepassdroid.database.PwEntryV3;
+import com.keepassdroid.database.PwEntry;
+import com.keepassdroid.database.PwGroup;
import com.keepassdroid.database.PwGroupV3;
import com.keepassdroid.database.edit.DeleteGroup;
import com.keepassdroid.search.SearchDbHelper;
@@ -54,8 +54,8 @@ public class DeleteEntry extends AndroidTestCase {
return;
}
-
- PwGroupV3 group1 = getGroup(db.pm, GROUP1_NAME);
+ PwDatabaseV3 pm = (PwDatabaseV3) db.pm;
+ PwGroup group1 = getGroup(pm, GROUP1_NAME);
assertNotNull("Could not find group1", group1);
// Delete the group
@@ -63,10 +63,10 @@ public class DeleteEntry extends AndroidTestCase {
task.run();
// Verify the entries were deleted
- PwEntryV3 entry1 = getEntry(db.pm, ENTRY1_NAME);
+ PwEntry entry1 = getEntry(pm, ENTRY1_NAME);
assertNull("Entry 1 was not removed", entry1);
- PwEntryV3 entry2 = getEntry(db.pm, ENTRY2_NAME);
+ PwEntry entry2 = getEntry(pm, ENTRY2_NAME);
assertNull("Entry 2 was not removed", entry2);
// Verify the entries were removed from the search index
@@ -80,17 +80,17 @@ public class DeleteEntry extends AndroidTestCase {
assertEquals("Entry2 was not removed from the search results", 0, results2.childEntries.size());
// Verify the group was deleted
- group1 = getGroup(db.pm, GROUP1_NAME);
+ group1 = getGroup(pm, GROUP1_NAME);
assertNull("Group 1 was not removed.", group1);
}
- private PwEntryV3 getEntry(PwDatabaseV3 pm, String name) {
- Vector entries = pm.entries;
+ private PwEntry getEntry(PwDatabaseV3 pm, String name) {
+ Vector entries = pm.entries;
for ( int i = 0; i < entries.size(); i++ ) {
- PwEntryV3 entry = entries.get(i);
+ PwEntry entry = entries.get(i);
if ( entry.title.equals(name) ) {
return entry;
}
@@ -100,11 +100,11 @@ public class DeleteEntry extends AndroidTestCase {
}
- private PwGroupV3 getGroup(PwDatabaseV3 pm, String name) {
- Vector groups = pm.groups;
+ private PwGroup getGroup(PwDatabaseV3 pm, String name) {
+ Vector groups = pm.getGroups();
for ( int i = 0; i < groups.size(); i++ ) {
- PwGroupV3 group = groups.get(i);
- if ( group.name.equals(name) ) {
+ PwGroup group = groups.get(i);
+ if ( group.getName().equals(name) ) {
return group;
}
}
diff --git a/tests/src/com/keepassdroid/tests/database/Kdb4.java b/tests/src/com/keepassdroid/tests/database/Kdb4.java
index 0070bf65c..1bb16a832 100644
--- a/tests/src/com/keepassdroid/tests/database/Kdb4.java
+++ b/tests/src/com/keepassdroid/tests/database/Kdb4.java
@@ -44,6 +44,9 @@ public class Kdb4 extends AndroidTestCase {
return;
}
+
+ is.close();
+
assertTrue(false);
}
diff --git a/tests/src/com/keepassdroid/tests/database/Kdb4Header.java b/tests/src/com/keepassdroid/tests/database/Kdb4Header.java
index d466c197b..7d3cc2fe2 100644
--- a/tests/src/com/keepassdroid/tests/database/Kdb4Header.java
+++ b/tests/src/com/keepassdroid/tests/database/Kdb4Header.java
@@ -44,7 +44,7 @@ public class Kdb4Header extends AndroidTestCase {
assertTrue(db.dataCipher.equals(CipherFactory.AES_CIPHER));
- am.close();
+ is.close();
}
}
diff --git a/tests/src/com/keepassdroid/tests/database/TestData.java b/tests/src/com/keepassdroid/tests/database/TestData.java
index 1ff4fba88..f748073cc 100644
--- a/tests/src/com/keepassdroid/tests/database/TestData.java
+++ b/tests/src/com/keepassdroid/tests/database/TestData.java
@@ -65,6 +65,6 @@ public class TestData {
GetDb1(ctx);
}
- return mDb1.pm;
+ return (PwDatabaseV3) mDb1.pm;
}
}
diff --git a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java
index b41d233e0..b3834a716 100644
--- a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java
+++ b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java
@@ -37,7 +37,7 @@ import com.keepassdroid.database.PwDatabaseV3;
import com.keepassdroid.database.PwDbHeader;
import com.keepassdroid.database.PwDbHeaderV3;
import com.keepassdroid.database.exception.PwDbOutputException;
-import com.keepassdroid.database.save.PwDbHeaderOutput;
+import com.keepassdroid.database.save.PwDbHeaderOutputV3;
import com.keepassdroid.database.save.PwDbV3Output;
import com.keepassdroid.stream.NullOutputStream;
import com.keepassdroid.tests.database.TestData;
@@ -99,7 +99,7 @@ public class PwManagerOutputTest extends AndroidTestCase {
PwDbHeaderV3 header = pActual.outputHeader(bActual);
ByteArrayOutputStream bExpected = new ByteArrayOutputStream();
- PwDbHeaderOutput outExpected = new PwDbHeaderOutput(mPM.dbHeader, bExpected);
+ PwDbHeaderOutputV3 outExpected = new PwDbHeaderOutputV3(mPM.dbHeader, bExpected);
outExpected.output();
assertHeadersEquals(mPM.dbHeader, header);