Make the API of all encoding classes similar

This commit is contained in:
Alexander Bakker 2018-03-19 18:33:38 +01:00
parent 9c433f96cf
commit 0ad39ab673
5 changed files with 12 additions and 12 deletions

View file

@ -32,8 +32,8 @@ public class DatabaseFile {
JSONObject cryptObj = null; JSONObject cryptObj = null;
if (_cryptParameters != null) { if (_cryptParameters != null) {
cryptObj = new JSONObject(); cryptObj = new JSONObject();
cryptObj.put("nonce", Hex.toString(_cryptParameters.Nonce)); cryptObj.put("nonce", Hex.encode(_cryptParameters.Nonce));
cryptObj.put("tag", Hex.toString(_cryptParameters.Tag)); cryptObj.put("tag", Hex.encode(_cryptParameters.Tag));
} }
// don't write the crypt parameters if the content is not encrypted // don't write the crypt parameters if the content is not encrypted
@ -70,8 +70,8 @@ public class DatabaseFile {
JSONObject cryptObj = headerObj.optJSONObject("params"); JSONObject cryptObj = headerObj.optJSONObject("params");
if (cryptObj != null) { if (cryptObj != null) {
_cryptParameters = new CryptParameters() {{ _cryptParameters = new CryptParameters() {{
Nonce = Hex.toBytes(cryptObj.getString("nonce")); Nonce = Hex.decode(cryptObj.getString("nonce"));
Tag = Hex.toBytes(cryptObj.getString("tag")); Tag = Hex.decode(cryptObj.getString("tag"));
}}; }};
} }

View file

@ -29,7 +29,7 @@ public class PasswordSlot extends RawSlot {
obj.put("n", _n); obj.put("n", _n);
obj.put("r", _r); obj.put("r", _r);
obj.put("p", _p); obj.put("p", _p);
obj.put("salt", Hex.toString(_salt)); obj.put("salt", Hex.encode(_salt));
return obj; return obj;
} catch (JSONException e) { } catch (JSONException e) {
throw new SlotException(e); throw new SlotException(e);
@ -43,7 +43,7 @@ public class PasswordSlot extends RawSlot {
_n = obj.getInt("n"); _n = obj.getInt("n");
_r = obj.getInt("r"); _r = obj.getInt("r");
_p = obj.getInt("p"); _p = obj.getInt("p");
_salt = Hex.toBytes(obj.getString("salt")); _salt = Hex.decode(obj.getString("salt"));
} catch (JSONException | HexException e) { } catch (JSONException | HexException e) {
throw new SlotException(e); throw new SlotException(e);
} }

View file

@ -72,7 +72,7 @@ public abstract class Slot implements Serializable {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("type", getType()); obj.put("type", getType());
obj.put("uuid", _uuid.toString()); obj.put("uuid", _uuid.toString());
obj.put("key", Hex.toString(_encryptedMasterKey)); obj.put("key", Hex.encode(_encryptedMasterKey));
return obj; return obj;
} catch (JSONException e) { } catch (JSONException e) {
throw new SlotException(e); throw new SlotException(e);
@ -90,7 +90,7 @@ public abstract class Slot implements Serializable {
} else { } else {
_uuid = UUID.fromString(obj.getString("uuid")); _uuid = UUID.fromString(obj.getString("uuid"));
} }
_encryptedMasterKey = Hex.toBytes(obj.getString("key")); _encryptedMasterKey = Hex.decode(obj.getString("key"));
} catch (JSONException | HexException e) { } catch (JSONException | HexException e) {
throw new SlotException(e); throw new SlotException(e);
} }

View file

@ -22,7 +22,7 @@ public class SlotCollection implements Iterable<Slot>, Serializable {
public static JSONObject serialize(SlotCollection slots) throws SlotCollectionException { public static JSONObject serialize(SlotCollection slots) throws SlotCollectionException {
try { try {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("hash", Hex.toString(slots.getMasterHash())); obj.put("hash", Hex.encode(slots.getMasterHash()));
JSONArray entries = new JSONArray(); JSONArray entries = new JSONArray();
for (Slot slot : slots) { for (Slot slot : slots) {
@ -40,7 +40,7 @@ public class SlotCollection implements Iterable<Slot>, Serializable {
SlotCollection slots = new SlotCollection(); SlotCollection slots = new SlotCollection();
try { try {
byte[] masterHash = Hex.toBytes(obj.getString("hash")); byte[] masterHash = Hex.decode(obj.getString("hash"));
slots.setMasterHash(masterHash); slots.setMasterHash(masterHash);
JSONArray entries = obj.getJSONArray("entries"); JSONArray entries = obj.getJSONArray("entries");

View file

@ -15,7 +15,7 @@ public class Hex {
private static final char[] hexCode = "0123456789abcdef".toCharArray(); private static final char[] hexCode = "0123456789abcdef".toCharArray();
public static byte[] toBytes(String s) throws HexException { public static byte[] decode(String s) throws HexException {
final int len = s.length(); final int len = s.length();
if (len % 2 != 0) if (len % 2 != 0)
@ -35,7 +35,7 @@ public class Hex {
return out; return out;
} }
public static String toString(byte[] data) { public static String encode(byte[] data) {
StringBuilder r = new StringBuilder(data.length * 2); StringBuilder r = new StringBuilder(data.length * 2);
for (byte b : data) { for (byte b : data) {
r.append(hexCode[(b >> 4) & 0xF]); r.append(hexCode[(b >> 4) & 0xF]);