-
Notifications
You must be signed in to change notification settings - Fork 80
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
||
public SecurePreferencesException(Throwable e) { | ||
super(e); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collapsed |
||
throw new SecurePreferencesException(e); | ||
} | ||
} | ||
|
||
protected void initCiphers(String secureKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
else { | ||
putValue(toKey(key), value); | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public String getString(String key) throws SecurePreferencesException { | ||
|
@@ -144,7 +140,7 @@ public String getString(String key) throws SecurePreferencesException { | |
} | ||
|
||
public void clear() { | ||
preferences.edit().clear().commit(); | ||
preferences.edit().clear().apply(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private String toKey(String key) { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
protected String encrypt(String value, Cipher writer) throws SecurePreferencesException { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplified code to improve performances. |
||
} | ||
|
||
protected String decrypt(String securedEncodedValue) { | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leaved |
||
try { | ||
return cipher.doFinal(bs); | ||
} | ||
|
There was a problem hiding this comment.
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