Skip to content

Commit

Permalink
Merge pull request #42 from orhanobut/oo/backup-algorithm
Browse files Browse the repository at this point in the history
backup algorithm improvement
  • Loading branch information
orhanobut committed May 29, 2015
2 parents c9bcc0c + 87ffeb7 commit 6d1c8aa
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Hawk provides:

###Add dependency
```groovy
compile 'com.orhanobut:hawk:1.10'
compile 'com.orhanobut:hawk:1.12'
```

#### Initialize the hawk
Expand Down
45 changes: 26 additions & 19 deletions app/src/main/java/com/orhanobut/hawksample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected void tearDown() throws Exception {

public void testBoolean() {
Hawk.put("tag", true);
Hawk.init(context);
assertEquals(true, Hawk.get("tag"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions hawk/src/main/java/com/orhanobut/hawk/AesEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 6d1c8aa

Please sign in to comment.