Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some bugs and improved performances #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions src/SecurePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ a copy of this software and associated documentation files (the

public class SecurePreferences {

public static class SecurePreferencesException extends RuntimeException {
public class SecurePreferencesException extends RuntimeException {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaved static attribute: Innit classes cannot have static declaration


public SecurePreferencesException(Throwable e) {
super(e);
Expand Down Expand Up @@ -82,12 +82,9 @@ public SecurePreferences(Context context, String preferenceName, String secureKe

this.encryptKeys = encryptKeys;
}
catch (GeneralSecurityException e) {
throw new SecurePreferencesException(e);
}
catch (UnsupportedEncodingException e) {
throw new SecurePreferencesException(e);
}
catch (GeneralSecurityException | UnsupportedEncodingException e) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collapsed catch() {clauses: more fast and stable

throw new SecurePreferencesException(e);
}
}

protected void initCiphers(String secureKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException,
Expand All @@ -114,13 +111,12 @@ protected SecretKeySpec getSecretKey(String key) throws UnsupportedEncodingExcep
protected byte[] createKeyBytes(String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(SECRET_KEY_HASH_TRANSFORMATION);
md.reset();
byte[] keyBytes = md.digest(key.getBytes(CHARSET));
return keyBytes;
return md.digest(key.getBytes(CHARSET));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collapsed and simplified code to decrease the machine's resource consumption

}

public void put(String key, String value) {
if (value == null) {
preferences.edit().remove(toKey(key)).commit();
preferences.edit().remove(toKey(key)).apply();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply(); mechanism is more used and stable than commit();

}
else {
putValue(toKey(key), value);
Expand All @@ -132,7 +128,7 @@ public boolean containsKey(String key) {
}

public void removeValue(String key) {
preferences.edit().remove(toKey(key)).commit();
preferences.edit().remove(toKey(key)).apply();
Copy link
Author

@ghost ghost Jul 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply(); mechanism is more used and stable than commit();

}

public String getString(String key) throws SecurePreferencesException {
Expand All @@ -144,7 +140,7 @@ public String getString(String key) throws SecurePreferencesException {
}

public void clear() {
preferences.edit().clear().commit();
preferences.edit().clear().apply();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply(); mechanism is more used and stable than commit();

}

private String toKey(String key) {
Expand All @@ -156,7 +152,7 @@ private String toKey(String key) {
private void putValue(String key, String value) throws SecurePreferencesException {
String secureValueEncoded = encrypt(value, writer);

preferences.edit().putString(key, secureValueEncoded).commit();
preferences.edit().putString(key, secureValueEncoded).apply();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply(); mechanism is more used and stable than commit();

}

protected String encrypt(String value, Cipher writer) throws SecurePreferencesException {
Expand All @@ -167,8 +163,7 @@ protected String encrypt(String value, Cipher writer) throws SecurePreferencesEx
catch (UnsupportedEncodingException e) {
throw new SecurePreferencesException(e);
}
String secureValueEncoded = Base64.encodeToString(secureValue, Base64.NO_WRAP);
return secureValueEncoded;
return Base64.encodeToString(secureValue, Base64.NO_WRAP);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified code to improve performances.

}

protected String decrypt(String securedEncodedValue) {
Expand All @@ -182,7 +177,7 @@ protected String decrypt(String securedEncodedValue) {
}
}

private static byte[] convert(Cipher cipher, byte[] bs) throws SecurePreferencesException {
private byte[] convert(Cipher cipher, byte[] bs) throws SecurePreferencesException {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaved static attribute: Innit classes cannot have static declaration

try {
return cipher.doFinal(bs);
}
Expand Down