Replace hardcoded encoding names with constants

This commit is contained in:
Alexander Bakker 2019-03-28 20:00:32 +01:00
parent e1fd913817
commit fc0e1150f6
4 changed files with 15 additions and 22 deletions

View file

@ -6,7 +6,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -122,7 +122,7 @@ public class CryptoUtils {
private static byte[] toBytes(char[] chars) { private static byte[] toBytes(char[] chars) {
CharBuffer charBuf = CharBuffer.wrap(chars); CharBuffer charBuf = CharBuffer.wrap(chars);
ByteBuffer byteBuf = Charset.forName("UTF-8").encode(charBuf); ByteBuffer byteBuf = StandardCharsets.UTF_8.encode(charBuf);
return byteBuf.array(); return byteBuf.array();
} }
} }

View file

@ -12,7 +12,7 @@ import com.beemdevelopment.aegis.encoding.HexException;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
public class DatabaseFile { public class DatabaseFile {
public static final byte VERSION = 1; public static final byte VERSION = 1;
@ -54,8 +54,8 @@ public class DatabaseFile {
try { try {
String string = obj.toString(4); String string = obj.toString(4);
return string.getBytes("UTF-8"); return string.getBytes(StandardCharsets.UTF_8);
} catch (JSONException | UnsupportedEncodingException e) { } catch (JSONException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -79,9 +79,9 @@ public class DatabaseFile {
public static DatabaseFile fromBytes(byte[] data) throws DatabaseFileException { public static DatabaseFile fromBytes(byte[] data) throws DatabaseFileException {
try { try {
JSONObject obj = new JSONObject(new String(data, "UTF-8")); JSONObject obj = new JSONObject(new String(data, StandardCharsets.UTF_8));
return DatabaseFile.fromJson(obj); return DatabaseFile.fromJson(obj);
} catch (UnsupportedEncodingException | JSONException e) { } catch (JSONException e) {
throw new DatabaseFileException(e); throw new DatabaseFileException(e);
} }
} }
@ -94,8 +94,8 @@ public class DatabaseFile {
try { try {
byte[] bytes = Base64.decode((String) _content); byte[] bytes = Base64.decode((String) _content);
CryptResult result = creds.decrypt(bytes, _header.getParams()); CryptResult result = creds.decrypt(bytes, _header.getParams());
return new JSONObject(new String(result.getData(), "UTF-8")); return new JSONObject(new String(result.getData(), StandardCharsets.UTF_8));
} catch (MasterKeyException | JSONException | UnsupportedEncodingException | Base64Exception e) { } catch (MasterKeyException | JSONException | Base64Exception e) {
throw new DatabaseFileException(e); throw new DatabaseFileException(e);
} }
} }
@ -108,12 +108,12 @@ public class DatabaseFile {
public void setContent(JSONObject obj, DatabaseFileCredentials creds) throws DatabaseFileException { public void setContent(JSONObject obj, DatabaseFileCredentials creds) throws DatabaseFileException {
try { try {
String string = obj.toString(4); String string = obj.toString(4);
byte[] dbBytes = string.getBytes("UTF-8"); byte[] dbBytes = string.getBytes(StandardCharsets.UTF_8);
CryptResult result = creds.encrypt(dbBytes); CryptResult result = creds.encrypt(dbBytes);
_content = Base64.encode(result.getData()); _content = Base64.encode(result.getData());
_header = new Header(creds.getSlots(), result.getParams()); _header = new Header(creds.getSlots(), result.getParams());
} catch (MasterKeyException | UnsupportedEncodingException | JSONException e) { } catch (MasterKeyException | JSONException e) {
throw new DatabaseFileException(e); throw new DatabaseFileException(e);
} }
} }

View file

@ -1,6 +1,6 @@
package com.beemdevelopment.aegis.encoding; package com.beemdevelopment.aegis.encoding;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
public class Base64 { public class Base64 {
private static final int _flags = android.util.Base64.NO_WRAP; private static final int _flags = android.util.Base64.NO_WRAP;
@ -19,11 +19,6 @@ public class Base64 {
public static String encode(byte[] data) { public static String encode(byte[] data) {
byte[] encoded = android.util.Base64.encode(data, _flags); byte[] encoded = android.util.Base64.encode(data, _flags);
return new String(encoded, StandardCharsets.UTF_8);
try {
return new String(encoded, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} }
} }

View file

@ -6,7 +6,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -29,11 +29,9 @@ public class AndOtpFileImporter extends DatabaseFileImporter {
@Override @Override
public void parse() throws DatabaseImporterException { public void parse() throws DatabaseImporterException {
try { try {
_obj = new JSONArray(new String(_stream.getBytes(), "UTF-8")); _obj = new JSONArray(new String(_stream.getBytes(), StandardCharsets.UTF_8));
} catch (JSONException e) { } catch (JSONException e) {
throw new DatabaseImporterException(e); throw new DatabaseImporterException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} }
} }