mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 22:12:55 +00:00
Improve database file reading code
This commit is contained in:
parent
4e10e5d514
commit
515e3a24eb
3 changed files with 27 additions and 12 deletions
|
@ -22,6 +22,7 @@ import me.impy.aegis.crypto.slots.Slot;
|
||||||
import me.impy.aegis.crypto.slots.SlotCollection;
|
import me.impy.aegis.crypto.slots.SlotCollection;
|
||||||
import me.impy.aegis.db.Database;
|
import me.impy.aegis.db.Database;
|
||||||
import me.impy.aegis.db.DatabaseFile;
|
import me.impy.aegis.db.DatabaseFile;
|
||||||
|
import me.impy.aegis.db.DatabaseManager;
|
||||||
|
|
||||||
public class IntroActivity extends AppIntro implements DerivationTask.Callback {
|
public class IntroActivity extends AppIntro implements DerivationTask.Callback {
|
||||||
public static final int RESULT_OK = 0;
|
public static final int RESULT_OK = 0;
|
||||||
|
@ -170,7 +171,7 @@ public class IntroActivity extends AppIntro implements DerivationTask.Callback {
|
||||||
_databaseFile.setContent(result.Data);
|
_databaseFile.setContent(result.Data);
|
||||||
_databaseFile.setCryptParameters(result.Parameters);
|
_databaseFile.setCryptParameters(result.Parameters);
|
||||||
}
|
}
|
||||||
_databaseFile.save(getApplicationContext());
|
_databaseFile.save(getApplicationContext(), DatabaseManager.FILENAME);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
setException(e);
|
setException(e);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package me.impy.aegis.db;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
@ -20,7 +21,6 @@ public class DatabaseFile {
|
||||||
private static final byte SECTION_SLOTS = 0x01;
|
private static final byte SECTION_SLOTS = 0x01;
|
||||||
private static final byte SECTION_END = (byte) 0xFF;
|
private static final byte SECTION_END = (byte) 0xFF;
|
||||||
private static final byte VERSION = 1;
|
private static final byte VERSION = 1;
|
||||||
private static final String FILENAME = "aegis.db";
|
|
||||||
|
|
||||||
private final byte[] HEADER;
|
private final byte[] HEADER;
|
||||||
|
|
||||||
|
@ -116,22 +116,34 @@ public class DatabaseFile {
|
||||||
return !_slots.isEmpty() && _cryptParameters != null;
|
return !_slots.isEmpty() && _cryptParameters != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(Context context) throws IOException {
|
public void save(Context context, String filename) throws IOException {
|
||||||
byte[] data = serialize();
|
byte[] data = serialize();
|
||||||
|
|
||||||
FileOutputStream file = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
|
FileOutputStream file = context.openFileOutput(filename, Context.MODE_PRIVATE);
|
||||||
file.write(data);
|
file.write(data);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DatabaseFile load(Context context) throws Exception {
|
public static DatabaseFile load(Context context, String filename) throws Exception {
|
||||||
FileInputStream file = context.openFileInput(FILENAME);
|
byte[] bytes;
|
||||||
byte[] data = new byte[(int) file.getChannel().size()];
|
FileInputStream file = null;
|
||||||
file.read(data);
|
|
||||||
file.close();
|
try {
|
||||||
|
file = context.openFileInput(filename);
|
||||||
|
DataInputStream stream = new DataInputStream(file);
|
||||||
|
bytes = new byte[(int) file.getChannel().size()];
|
||||||
|
stream.readFully(bytes);
|
||||||
|
stream.close();
|
||||||
|
} finally {
|
||||||
|
// always close the file
|
||||||
|
// there is no need to close the DataInputStream
|
||||||
|
if (file != null) {
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DatabaseFile db = new DatabaseFile();
|
DatabaseFile db = new DatabaseFile();
|
||||||
db.deserialize(data);
|
db.deserialize(bytes);
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ import me.impy.aegis.crypto.CryptResult;
|
||||||
import me.impy.aegis.crypto.MasterKey;
|
import me.impy.aegis.crypto.MasterKey;
|
||||||
|
|
||||||
public class DatabaseManager {
|
public class DatabaseManager {
|
||||||
|
public static final String FILENAME = "aegis.db";
|
||||||
|
|
||||||
private MasterKey _key;
|
private MasterKey _key;
|
||||||
private DatabaseFile _file;
|
private DatabaseFile _file;
|
||||||
private Database _db;
|
private Database _db;
|
||||||
|
@ -20,7 +22,7 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public void load() throws Exception {
|
||||||
_file = DatabaseFile.load(_context);
|
_file = DatabaseFile.load(_context, FILENAME);
|
||||||
if (!_file.isEncrypted()) {
|
if (!_file.isEncrypted()) {
|
||||||
byte[] bytes = _file.getContent();
|
byte[] bytes = _file.getContent();
|
||||||
_db = new Database();
|
_db = new Database();
|
||||||
|
@ -48,7 +50,7 @@ public class DatabaseManager {
|
||||||
_file.setContent(result.Data);
|
_file.setContent(result.Data);
|
||||||
_file.setCryptParameters(result.Parameters);
|
_file.setCryptParameters(result.Parameters);
|
||||||
}
|
}
|
||||||
_file.save(_context);
|
_file.save(_context, FILENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addKey(DatabaseEntry entry) throws Exception {
|
public void addKey(DatabaseEntry entry) throws Exception {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue