2016-08-22 19:31:03 +02:00
|
|
|
package me.impy.aegis.db;
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2016-08-22 19:31:03 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import me.impy.aegis.KeyProfile;
|
|
|
|
import me.impy.aegis.crypto.KeyInfo;
|
|
|
|
|
|
|
|
public class Database {
|
2016-11-13 18:21:00 +01:00
|
|
|
private static final int version = 1;
|
|
|
|
private List<DatabaseEntry> entries = new ArrayList<>();
|
|
|
|
|
|
|
|
public byte[] serialize() throws JSONException, UnsupportedEncodingException {
|
|
|
|
JSONArray array = new JSONArray();
|
|
|
|
for (DatabaseEntry e : entries) {
|
|
|
|
array.put(e.serialize());
|
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
obj.put("version", version);
|
|
|
|
obj.put("entries", array);
|
|
|
|
|
|
|
|
return obj.toString().getBytes("UTF-8");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
public void deserialize(byte[] data) throws Exception {
|
|
|
|
JSONObject obj = new JSONObject(new String(data, "UTF-8"));
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
// TODO: support different version deserialization providers
|
|
|
|
int ver = obj.getInt("version");
|
|
|
|
if (ver != version) {
|
|
|
|
throw new Exception("Unsupported version");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONArray array = obj.getJSONArray("entries");
|
|
|
|
for (int i = 0; i < array.length(); i++) {
|
|
|
|
DatabaseEntry e = new DatabaseEntry();
|
|
|
|
e.deserialize(array.getJSONObject(i));
|
|
|
|
entries.add(e);
|
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addKey(KeyProfile profile) throws Exception {
|
2016-11-13 18:21:00 +01:00
|
|
|
DatabaseEntry e = new DatabaseEntry();
|
|
|
|
e.Name = profile.Name;
|
|
|
|
e.URL = profile.Info.getURL();
|
|
|
|
e.Order = profile.Order;
|
|
|
|
e.ID = entries.size() + 1;
|
|
|
|
profile.ID = e.ID;
|
|
|
|
entries.add(e);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKey(KeyProfile profile) throws Exception {
|
2016-11-13 18:21:00 +01:00
|
|
|
DatabaseEntry e = findEntry(profile);
|
|
|
|
e.Name = profile.Name;
|
|
|
|
e.URL = profile.Info.getURL();
|
|
|
|
e.Order = profile.Order;
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
public void removeKey(KeyProfile profile) throws Exception {
|
|
|
|
DatabaseEntry e = findEntry(profile);
|
|
|
|
entries.remove(e);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<KeyProfile> getKeys() throws Exception {
|
|
|
|
List<KeyProfile> list = new ArrayList<>();
|
2016-11-13 18:21:00 +01:00
|
|
|
|
|
|
|
for (DatabaseEntry e : entries) {
|
|
|
|
KeyProfile profile = new KeyProfile();
|
|
|
|
profile.Name = e.Name;
|
2017-08-26 15:47:57 +02:00
|
|
|
profile.Info = KeyInfo.fromURL(e.URL);
|
2016-11-13 18:21:00 +01:00
|
|
|
profile.Order = e.Order;
|
|
|
|
profile.ID = e.ID;
|
|
|
|
list.add(profile);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
return list;
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
private DatabaseEntry findEntry(KeyProfile profile) throws Exception {
|
|
|
|
for (DatabaseEntry e : entries) {
|
|
|
|
if (e.ID == profile.ID) {
|
|
|
|
return e;
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-13 18:21:00 +01:00
|
|
|
|
|
|
|
throw new Exception("Key doesn't exist");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
2017-05-03 21:08:38 +02:00
|
|
|
}
|