diff --git a/README.md b/README.md index c58412e..3bcac79 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Hawk provides: ###Add dependency ```groovy -compile 'com.orhanobut:hawk:1.10' +compile 'com.orhanobut:hawk:1.12' ``` #### Initialize the hawk diff --git a/app/src/main/java/com/orhanobut/hawksample/MainActivity.java b/app/src/main/java/com/orhanobut/hawksample/MainActivity.java index 7a0edb9..0a87529 100644 --- a/app/src/main/java/com/orhanobut/hawksample/MainActivity.java +++ b/app/src/main/java/com/orhanobut/hawksample/MainActivity.java @@ -8,7 +8,6 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.orhanobut.hawk.Hawk; -import com.orhanobut.hawk.LogLevel; import java.lang.reflect.Type; import java.util.ArrayList; @@ -27,25 +26,33 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Hawk.init(this, "asdfasdfds", LogLevel.FULL); - - prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE); - editor = prefs.edit(); - - benchmarkPrimitivePut(); - benchmarkStringPut(); - benchmarkListObjectPut(); - benchmarkListStringPut(); - benchmarkObjectPut(); - - - benchmarkPrimitiveGet(); - benchmarkStringGet(); - benchmarkListObjectGet(); - benchmarkListStringGet(); - benchmarkObjectGet(); + // Hawk.init(this, "asdfasdfds", LogLevel.FULL); + // + // prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE); + // editor = prefs.edit(); + // + // benchmarkPrimitivePut(); + // benchmarkStringPut(); + // benchmarkListObjectPut(); + // benchmarkListStringPut(); + // benchmarkObjectPut(); + // + // + // benchmarkPrimitiveGet(); + // benchmarkStringGet(); + // benchmarkListObjectGet(); + // benchmarkListStringGet(); + // benchmarkObjectGet(); + // + // benchmarkDelete(); + testHawkInitWithoutPassword(); + } - benchmarkDelete(); + private void testHawkInitWithoutPassword() { + Hawk.init(this); + Hawk.put("test123", "test"); + Hawk.init(this); + Hawk.get("test123"); } private static final String KEY = "KEY"; diff --git a/gradle.properties b/gradle.properties index aa23194..3dcf163 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,10 +17,10 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -# VERSION_NAME=1.9-SNAPSHOT -# VERSION_CODE=10 -VERSION_NAME=1.10 -VERSION_CODE=11 +# VERSION_NAME=1.10-SNAPSHOT +# VERSION_CODE=11 +VERSION_NAME=1.12 +VERSION_CODE=13 GROUP=com.orhanobut POM_DESCRIPTION=Secure, Advanced Storage for android diff --git a/hawk/src/androidTest/java/com/orhanobut/hawk/HawkBackupTest.java b/hawk/src/androidTest/java/com/orhanobut/hawk/HawkBackupTest.java index e5a8492..48c5b9f 100644 --- a/hawk/src/androidTest/java/com/orhanobut/hawk/HawkBackupTest.java +++ b/hawk/src/androidTest/java/com/orhanobut/hawk/HawkBackupTest.java @@ -33,6 +33,7 @@ protected void tearDown() throws Exception { public void testBoolean() { Hawk.put("tag", true); + Hawk.init(context); assertEquals(true, Hawk.get("tag")); } diff --git a/hawk/src/main/java/com/orhanobut/hawk/AesCbcWithIntegrity.java b/hawk/src/main/java/com/orhanobut/hawk/AesCbcWithIntegrity.java index eef6ae1..d363acd 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/AesCbcWithIntegrity.java +++ b/hawk/src/main/java/com/orhanobut/hawk/AesCbcWithIntegrity.java @@ -217,7 +217,7 @@ public static byte[] generateIv() throws GeneralSecurityException { private static byte[] randomBytes(int length) throws GeneralSecurityException { fixPrng(); - SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM); + SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM, "Crypto"); byte[] b = new byte[length]; random.nextBytes(b); return b; @@ -443,6 +443,7 @@ public void setIntegrityKey(SecretKey integrityKey) { public String toString() { return Base64.encodeToString(getConfidentialityKey().getEncoded(), BASE64_FLAGS) + ":" + Base64.encodeToString(getIntegrityKey().getEncoded(), BASE64_FLAGS); + } @Override diff --git a/hawk/src/main/java/com/orhanobut/hawk/AesEncryption.java b/hawk/src/main/java/com/orhanobut/hawk/AesEncryption.java index 0c54a3c..770d43f 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/AesEncryption.java +++ b/hawk/src/main/java/com/orhanobut/hawk/AesEncryption.java @@ -18,14 +18,12 @@ final class AesEncryption implements Encryption { private final String password; private final Storage storage; - private final Encoder encoder; private AesCbcWithIntegrity.SecretKeys key; private String saltKey; - AesEncryption(Storage storage, Encoder encoder, String password) { + AesEncryption(Storage storage, String password) { this.storage = storage; - this.encoder = encoder; this.password = password; } @@ -95,29 +93,36 @@ private AesCbcWithIntegrity.CipherTextIvMac getCipherTextIvMac(String cipherText private void generateSecretKey(String password) throws GeneralSecurityException { if (password == null || storage.contains(KEY_GENERATED_SECRET_KEYS)) { key = getSecretKeysWithoutPassword(); - Logger.w("key is generated without password"); return; } key = generateSecretKeyFromPassword(password); if (key == null) { - key = AesCbcWithIntegrity.generateKey(); - storage.put(KEY_GENERATED_SECRET_KEYS, encoder.encode(key)); + key = getSecretKeysWithoutPassword(); + } else { + Logger.w("key is generated from password"); } - Logger.w("key is generated from password"); } private AesCbcWithIntegrity.SecretKeys getSecretKeysWithoutPassword() { + Logger.w("key is generating without password"); try { AesCbcWithIntegrity.SecretKeys key = null; String keys = storage.get(KEY_GENERATED_SECRET_KEYS); if (keys != null) { - key = encoder.decodeSerializable(keys); + try { + key = AesCbcWithIntegrity.keys(keys); + } catch (Exception e) { + key = null; + Logger.i("keys was not correct value, it is reset"); + } } if (key == null) { key = AesCbcWithIntegrity.generateKey(); - storage.put(KEY_GENERATED_SECRET_KEYS, encoder.encode(key)); + String parsed = key.toString(); + storage.put(KEY_GENERATED_SECRET_KEYS, parsed); } + Logger.w("key is generated without password"); return key; } catch (GeneralSecurityException e) { Logger.e(e.getMessage()); diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index 10d0f7c..ac00b93 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -97,7 +97,7 @@ public static void init(Context context, String password, LogLevel logLevel) { } Storage cryptoStorage = new SharedPreferencesStorage(appContext, TAG_CRYPTO); - Hawk.encryption = new AesEncryption(cryptoStorage, encoder, password); + Hawk.encryption = new AesEncryption(cryptoStorage, password); boolean result = Hawk.encryption.init(); setEncryptionMode(result); }