From 23b4aab520a95a8de9d05466b22ef43638a93a06 Mon Sep 17 00:00:00 2001 From: orhanobut Date: Sat, 30 May 2015 18:12:22 +0200 Subject: [PATCH] v1.14 --- README.md | 2 +- gradle.properties | 4 ++-- .../java/com/orhanobut/hawk/DataUtil.java | 20 +++++++++++++++++++ .../main/java/com/orhanobut/hawk/Hawk.java | 9 +++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e477b1..7370544 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Hawk provides: ###Add dependency ```groovy -compile 'com.orhanobut:hawk:1.13' +compile 'com.orhanobut:hawk:1.14' ``` #### Initialize the hawk diff --git a/gradle.properties b/gradle.properties index 7479056..cba6190 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,8 +19,8 @@ # VERSION_NAME=1.10-SNAPSHOT # VERSION_CODE=11 -VERSION_NAME=1.13 -VERSION_CODE=14 +VERSION_NAME=1.14 +VERSION_CODE=15 GROUP=com.orhanobut POM_DESCRIPTION=Secure, Advanced Storage for android diff --git a/hawk/src/main/java/com/orhanobut/hawk/DataUtil.java b/hawk/src/main/java/com/orhanobut/hawk/DataUtil.java index 61bc737..fa9c556 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/DataUtil.java +++ b/hawk/src/main/java/com/orhanobut/hawk/DataUtil.java @@ -1,5 +1,7 @@ package com.orhanobut.hawk; +import android.util.Base64; + import java.io.Serializable; /** @@ -75,5 +77,23 @@ static String addTypeAsList(String cipherText, Class clazz) { return className + TYPE_LIST + FLAG_NON_SERIALIZABLE + DELIMITER + cipherText; } + static String encodeBase64(byte[] bytes) { + try { + return Base64.encodeToString(bytes, Base64.DEFAULT); + } catch (Exception e) { + Logger.w(e.getMessage()); + return null; + } + } + + static byte[] decodeBase64(String value) { + try { + return Base64.decode(value, Base64.DEFAULT); + } catch (Exception e) { + Logger.w(e.getMessage()); + return null; + } + } + } diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index ac00b93..9580da8 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -241,7 +241,7 @@ private static String encode(List list) { String cipherText; if (noEncryption) { - cipherText = Base64.encodeToString(encodedValue, Base64.DEFAULT); + cipherText = DataUtil.encodeBase64(encodedValue); } else { cipherText = encryption.encrypt(encodedValue); } @@ -269,10 +269,15 @@ public static T get(String key) { byte[] bytes; if (noEncryption) { - bytes = Base64.decode(dataInfo.getCipherText(), Base64.DEFAULT); + bytes = DataUtil.decodeBase64(dataInfo.getCipherText()); } else { bytes = encryption.decrypt(dataInfo.getCipherText()); } + + if (bytes == null) { + return null; + } + try { return encoder.decode(bytes, dataInfo); } catch (Exception e) {