Skip to content

Commit

Permalink
Merge pull request #111 from carlonzo/hawk-internal
Browse files Browse the repository at this point in the history
Created HawkInternal
  • Loading branch information
orhanobut committed Dec 9, 2015
2 parents 646e7ab + 04f4c20 commit 93bba11
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 76 deletions.
22 changes: 22 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/Base64Encryption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.orhanobut.hawk;

/**
* Provides Base64 Algorithm
*/
public class Base64Encryption implements Encryption {
@Override public boolean init() {
return true;
}

@Override public String encrypt(byte[] value) {
return DataHelper.encodeBase64(value);
}

@Override public byte[] decrypt(String value) {
return DataHelper.decodeBase64(value);
}

@Override public boolean reset() {
return true;
}
}
53 changes: 23 additions & 30 deletions hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public final class Hawk {

private static HawkBuilder hawkBuilder;
private static HawkInternal internal;

private Hawk() {
// no instance
Expand All @@ -27,8 +27,13 @@ public static HawkBuilder init(Context context) {
if (context == null) {
throw new NullPointerException("Context should not be null");
}
hawkBuilder = new HawkBuilder(context);
return hawkBuilder;

internal = null;
return new HawkBuilder(context);
}

static void onHawkBuilt(HawkBuilder builder) {
Hawk.internal = new HawkInternal(builder);
}

/**
Expand All @@ -49,7 +54,7 @@ public static <T> boolean put(String key, T value) {

String encodedText = zip(value);
//if any exception occurs during encoding, encodedText will be null and thus operation is unsuccessful
return encodedText != null && hawkBuilder.getStorage().put(key, encodedText);
return encodedText != null && internal.getStorage().put(key, encodedText);
}

/**
Expand Down Expand Up @@ -88,15 +93,9 @@ private static <T> String zip(T value) {
if (value == null) {
throw new NullPointerException("Value cannot be null");
}
byte[] encodedValue = hawkBuilder.getEncoder().encode(value);
byte[] encodedValue = internal.getEncoder().encode(value);

String cipherText;

if (!hawkBuilder.isEncrypted()) {
cipherText = DataHelper.encodeBase64(encodedValue);
} else {
cipherText = hawkBuilder.getEncryption().encrypt(encodedValue);
}
String cipherText = internal.getEncryption().encrypt(encodedValue);

if (cipherText == null) {
return null;
Expand All @@ -112,25 +111,19 @@ public static <T> T get(String key) {
if (key == null) {
throw new NullPointerException("Key cannot be null");
}
String fullText = hawkBuilder.getStorage().get(key);
String fullText = internal.getStorage().get(key);
if (fullText == null) {
return null;
}
DataInfo dataInfo = DataHelper.getDataInfo(fullText);
byte[] bytes;

if (!hawkBuilder.isEncrypted()) {
bytes = DataHelper.decodeBase64(dataInfo.getCipherText());
} else {
bytes = hawkBuilder.getEncryption().decrypt(dataInfo.getCipherText());
}
byte[] bytes = internal.getEncryption().decrypt(dataInfo.getCipherText());

if (bytes == null) {
return null;
}

try {
return hawkBuilder.getEncoder().decode(bytes, dataInfo);
return internal.getEncoder().decode(bytes, dataInfo);
} catch (Exception e) {
Logger.d(e.getMessage());
}
Expand Down Expand Up @@ -219,7 +212,7 @@ public static Chain chain(int capacity) {
* @return the size
*/
public static long count() {
return hawkBuilder.getStorage().count();
return internal.getStorage().count();
}

/**
Expand All @@ -229,7 +222,7 @@ public static long count() {
* @return true if clear is successful
*/
public static boolean clear() {
return hawkBuilder.getStorage().clear();
return internal.getStorage().clear();
}

/**
Expand All @@ -239,7 +232,7 @@ public static boolean clear() {
* @return true if remove is successful
*/
public static boolean remove(String key) {
return hawkBuilder.getStorage().remove(key);
return internal.getStorage().remove(key);
}

/**
Expand All @@ -249,7 +242,7 @@ public static boolean remove(String key) {
* @return true if all removals are successful
*/
public static boolean remove(String... keys) {
return hawkBuilder.getStorage().remove(keys);
return internal.getStorage().remove(keys);
}

/**
Expand All @@ -259,7 +252,7 @@ public static boolean remove(String... keys) {
* @return true if it exists in the storage
*/
public static boolean contains(String key) {
return hawkBuilder.getStorage().contains(key);
return internal.getStorage().contains(key);
}

/**
Expand All @@ -268,14 +261,14 @@ public static boolean contains(String key) {
* @return true if reset is successful
*/
public static boolean resetCrypto() {
return hawkBuilder.getEncryption() == null || hawkBuilder.getEncryption().reset();
return internal.getEncryption().reset();
}

public static LogLevel getLogLevel() {
if (hawkBuilder == null) {
if (internal == null) {
return LogLevel.NONE;
}
return hawkBuilder.getLogLevel();
return internal.getLogLevel();
}

/**
Expand Down Expand Up @@ -322,7 +315,7 @@ public <T> Chain put(String key, T value) {
* @return true if successfully saved, false otherwise.
*/
public boolean commit() {
return hawkBuilder.getStorage().put(items);
return internal.getStorage().put(items);
}

}
Expand Down
32 changes: 12 additions & 20 deletions hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,61 +81,53 @@ public HawkBuilder setParser(Parser parser) {
return this;
}

public Context getContext() {
return context;
}

public EncryptionMethod getEncryptionMethod() {
EncryptionMethod getEncryptionMethod() {
if (encryptionMethod == null) {
encryptionMethod = EncryptionMethod.MEDIUM;
}
return encryptionMethod;
}

public String getPassword() {
String getPassword() {
return password;
}

public LogLevel getLogLevel() {
LogLevel getLogLevel() {
if (logLevel == null) {
logLevel = LogLevel.NONE;
}
return logLevel;
}

public Storage getStorage() {
Storage getStorage() {
if (cryptoStorage == null) {
cryptoStorage = new SharedPreferencesStorage(context, TAG);
}
return cryptoStorage;
}

public Encoder getEncoder() {
Encoder getEncoder() {
if (encoder == null) {
encoder = new HawkEncoder(getParser());
}
return encoder;
}

public Storage getInfoStorage() {
Storage getInfoStorage() {
return new SharedPreferencesStorage(context, TAG_INFO);
}

public Parser getParser() {
Parser getParser() {
if (parser == null) {
parser = new GsonParser(new Gson());
}
return parser;
}

public Encryption getEncryption() {
Encryption getEncryption() {
return encryption;
}

public boolean isEncrypted() {
return encryptionMethod != EncryptionMethod.NO_ENCRYPTION;
}

private void validate() {
if (getEncryptionMethod() == EncryptionMethod.HIGHEST) {
if (TextUtils.isEmpty(getPassword())) {
Expand Down Expand Up @@ -165,28 +157,28 @@ public void build() {
private void startBuild() {
validate();
setEncryption();
Hawk.onHawkBuilt(this);
}

private void setEncryption() {
switch (getEncryptionMethod()) {
case NO_ENCRYPTION:
encryption = new Base64Encryption();
break;
case HIGHEST:
encryption = new AesEncryption(getStorage(), getPassword());
if (!getEncryption().init()) {
getInfoStorage().put(KEY_NO_CRYPTO, true);
encryptionMethod = EncryptionMethod.NO_ENCRYPTION;
encryption = new Base64Encryption();
}
break;
case MEDIUM:
encryption = new AesEncryption(getStorage(), null);
if (!getEncryption().init()) {
getInfoStorage().put(KEY_NO_CRYPTO, true);
encryptionMethod = EncryptionMethod.NO_ENCRYPTION;
encryption = new Base64Encryption();
}
break;
default:
throw new IllegalStateException("Encryption mode is not correct");
}
}

Expand Down
6 changes: 3 additions & 3 deletions hawk/src/main/java/com/orhanobut/hawk/HawkEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private <T> T toList(String json, Class<?> type) throws Exception {
List<T> list = parser.fromJson(
json,
new TypeToken<List<T>>() {
} .getType()
}.getType()
);

int size = list.size();
Expand All @@ -124,7 +124,7 @@ private <T> T toSet(String json, Class<?> type) throws Exception {
return (T) resultSet;
}
Set<T> set = parser.fromJson(json, new TypeToken<Set<T>>() {
} .getType());
}.getType());

for (T t : set) {
String valueJson = parser.toJson(t);
Expand All @@ -141,7 +141,7 @@ private <K, V, T> T toMap(String json, Class<?> keyType, Class<?> valueType) thr
return (T) resultMap;
}
Map<K, V> map = parser.fromJson(json, new TypeToken<Map<K, V>>() {
} .getType());
}.getType());

for (Map.Entry<K, V> entry : map.entrySet()) {
String keyJson = parser.toJson(entry.getKey());
Expand Down
33 changes: 33 additions & 0 deletions hawk/src/main/java/com/orhanobut/hawk/HawkInternal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.orhanobut.hawk;

public class HawkInternal {

private Storage cryptoStorage;
private Encoder encoder;
private Encryption encryption;
private LogLevel logLevel;

HawkInternal(HawkBuilder hawkBuilder) {
cryptoStorage = hawkBuilder.getStorage();
encoder = hawkBuilder.getEncoder();
encryption = hawkBuilder.getEncryption();
logLevel = hawkBuilder.getLogLevel();
}

Storage getStorage() {
return cryptoStorage;
}

Encoder getEncoder() {
return encoder;
}

Encryption getEncryption() {
return encryption;
}

LogLevel getLogLevel() {
return logLevel;
}

}
6 changes: 3 additions & 3 deletions hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.util.Base64;

import junit.framework.TestCase;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
Expand All @@ -16,11 +14,13 @@
import java.util.Map;
import java.util.Set;

import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 18)
public class DataHelperTest extends TestCase {
public class DataHelperTest {

private static final String CIPHER_TEXT = "CIPHER";

Expand Down
Loading

0 comments on commit 93bba11

Please sign in to comment.