-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 87b39d3
Showing
56 changed files
with
1,727 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
[![Release](https://jitpack.io/v/jitpack/android-example.svg)](https://jitpack.io/#rommansabbir/StoreX) | ||
# StoreX | ||
✔️ Simplify Caching in Android | ||
|
||
## Features | ||
* Notify on Data Changes based on Subscription | ||
* Lightweight | ||
* AES Encryption | ||
* Thread Safe | ||
|
||
## How does it work? | ||
Caching is just a simple key-value pair data saving procedure. StoreX follows the same approach. StoreX uses SharedPreference as storage for caching data. Since we really can't just save the original data because of security issues. StoreX uses AES encryption & decryption behind the scene when you are caching data or fetching data from the cache. Also, you can observer cached data in real-time. | ||
|
||
## Documentation | ||
|
||
### Installation | ||
|
||
--- | ||
Step 1. Add the JitPack repository to your build file | ||
|
||
```gradle | ||
allprojects { | ||
repositories { | ||
maven { url 'https://jitpack.io' } | ||
} | ||
} | ||
``` | ||
|
||
Step 2. Add the dependency | ||
|
||
```gradle | ||
dependencies { | ||
implementation 'com.github.rommansabbir:StoreX:Tag' | ||
} | ||
``` | ||
|
||
--- | ||
|
||
### Version available | ||
|
||
| Latest Releases | ||
| ------------- | | ||
| 1.0.0 | | ||
|
||
--- | ||
|
||
## Initialize | ||
```` | ||
StoreXCore.init(application: Application, prefName: String) | ||
```` | ||
|
||
## How To Access | ||
```` | ||
StoreXCore.instance() | ||
```` | ||
or else, use the extension function | ||
```` | ||
storeXInstance() | ||
```` | ||
which return an instance of `StoreX` [Note: You must initalize `StoreX` properly before accessing or else it will throw `NotInitializedException`] | ||
|
||
Also, you can set encryption key for your data accross the application by calling this method `StoreXCore.setEncryptionKey(key:String)` which throw `InvalidEncryptionKeyException` if you passed empty `String` | ||
|
||
---- | ||
|
||
# Usages | ||
|
||
## How to save an object to StoreX? | ||
|
||
Create any data class that extends `StoreAbleObject` | ||
Example: | ||
````` | ||
class MyClass:StoreAbleObject() | ||
````` | ||
How to save the object (Main Thread) | ||
```` | ||
StoreX.put(key: String, value: StoreAbleObject) | ||
```` | ||
or use Async | ||
```` | ||
StoreX.put<T : StoreAbleObject>(key: String, value: StoreAbleObject, callback: SaveCallback<T>) | ||
```` | ||
|
||
|
||
## How to get an object from StoreX? | ||
Get an saved object (Main Thread) which may throw `RuntimeException` | ||
```` | ||
StoreX.get<T : StoreAbleObject>(key: String, objectType: Class<T>): T | ||
```` | ||
or use Async | ||
```` | ||
StoreX.<T : StoreAbleObject>get(key: String, objectType: Class<T>, callback: GetCallback<T>) | ||
```` | ||
|
||
## How to get notified on data changes? | ||
Simple, to get notified on any data changes according to a given key you first need to subscribe to StoreX by calling this method `StoreX.addSubscriber(subscriber: Subscriber)` else if you want to add a list subscriber use `StoreX.addSubscriber(subscribers: ArrayList<Subscriber>)` | ||
|
||
Example: | ||
First setup an callback for the data changes | ||
```` | ||
private val callback1 = object : EventCallback{ | ||
override fun onDataChanges(subscriber: Subscriber, instance: StoreX) { | ||
// Callback return an instance of StoreX and the specific subscriber | ||
} | ||
} | ||
```` | ||
Create a new sunscriber. | ||
|
||
Create a new subscriber by providing the `Key`, `Observer ID (Must be unique)` and the `Callback` | ||
|
||
```` | ||
private var subscriber1 = Subscriber(key, OBSERVER_ID_1, callback1) | ||
```` | ||
|
||
Now register the subscriber to the `StoreX` by calling this method | ||
```` | ||
StoreX.addSubscriber(subscriber1) | ||
```` | ||
|
||
Also, you can remove your subscription any time by calling this method | ||
`StoreX.removeSubscriber(subscriber: Subscriber)` or list of subscriber `StoreX.removeSubscriber(subscribers: ArrayList<Subscriber>)` | ||
```` | ||
storeXInstance().removeSubscriber(subscriber1) | ||
```` | ||
|
||
## How to remove any saved data? | ||
```` | ||
StoreX.remove(key: String) | ||
```` | ||
|
||
## How to remove all data when you are crazy | ||
```` | ||
StoreX.removeAll() | ||
```` | ||
|
||
##### Need more information regarding the solid implementation? - Check out the sample application. | ||
|
||
--- | ||
|
||
|
||
### How does StoreX work? | ||
|
||
<img src='https://github.com/rommansabbir/StoreX/blob/master/art/how-storex-works.png'/> | ||
|
||
---- | ||
|
||
### Contact me | ||
[Portfolio](https://www.rommansabbir.com/) | [LinkedIn](https://www.linkedin.com/in/rommansabbir/) | [Twitter](https://www.twitter.com/itzrommansabbir/) | [Facebook](https://www.facebook.com/itzrommansabbir/) | ||
|
||
### License | ||
|
||
--- | ||
[Apache Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) | ||
|
||
```` | ||
Copyright (C) 2021 Romman Sabbir | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
```` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
group = 'rommansabbir' | ||
version '1.0' | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.3.2' | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.material:material:1.3.0' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||
implementation "com.google.code.gson:gson:2.8.6" | ||
|
||
|
||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2' | ||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9' | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.rommansabbir.storex"> | ||
|
||
</manifest> |
101 changes: 101 additions & 0 deletions
101
StoreX/src/main/java/com/rommansabbir/storex/EncryptionTool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.rommansabbir.storex | ||
|
||
import android.util.Base64 | ||
import java.security.GeneralSecurityException | ||
import java.security.SecureRandom | ||
import javax.crypto.Cipher | ||
import javax.crypto.SecretKey | ||
import javax.crypto.SecretKeyFactory | ||
import javax.crypto.spec.IvParameterSpec | ||
import javax.crypto.spec.PBEKeySpec | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
|
||
internal object EncryptionTool { | ||
private const val ITERATION_COUNT = 1000 | ||
private const val KEY_LENGTH = 256 | ||
private const val PBKDF2_DERIVATION_ALGORITHM = "PBKDF2WithHmacSHA1" | ||
private const val CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding" | ||
private const val PKCS5_SALT_LENGTH = 32 | ||
private const val DELIMITER = "]" | ||
private val random = SecureRandom() | ||
|
||
/** | ||
* Encrypt the given data. | ||
* | ||
* @param dataToEncrypt Data that need to be encrypted | ||
* | ||
* @return [String] Encrypted Data | ||
*/ | ||
fun encrypt(dataToEncrypt: String): String { | ||
val salt = generateSalt() | ||
val key = deriveKey(StoreXCore.encryptionKey, salt) | ||
val cipher = Cipher.getInstance(CIPHER_ALGORITHM) | ||
val iv = generateIv(cipher.blockSize) | ||
val ivParams = IvParameterSpec(iv) | ||
cipher.init(Cipher.ENCRYPT_MODE, key, ivParams) | ||
val cipherText = cipher.doFinal(dataToEncrypt.toByteArray(Charsets.UTF_8)) | ||
return String.format( | ||
"%s%s%s%s%s", | ||
toBase64(salt), | ||
DELIMITER, | ||
toBase64(iv), | ||
DELIMITER, | ||
toBase64(cipherText) | ||
) | ||
} | ||
|
||
/** | ||
* Decrypt the given data. | ||
* | ||
* @param dataToDecrypt Data that need to be encrypted | ||
* | ||
* @return [String] Decrypted Data | ||
*/ | ||
fun decrypt(dataToDecrypt: String): String { | ||
val fields = | ||
dataToDecrypt.split(DELIMITER.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
require(fields.size == 3) { "Invalid encrypted text format" } | ||
val salt = fromBase64(fields[0]) | ||
val iv = fromBase64(fields[1]) | ||
val cipherBytes = fromBase64(fields[2]) | ||
val key = deriveKey(StoreXCore.encryptionKey, salt) | ||
val cipher = Cipher.getInstance(CIPHER_ALGORITHM) | ||
val ivParams = IvParameterSpec(iv) | ||
cipher.init(Cipher.DECRYPT_MODE, key, ivParams) | ||
val plaintext = cipher.doFinal(cipherBytes) | ||
return String(plaintext, Charsets.UTF_8) | ||
} | ||
|
||
private fun generateSalt(): ByteArray { | ||
val b = ByteArray(PKCS5_SALT_LENGTH) | ||
random.nextBytes(b) | ||
return b | ||
} | ||
|
||
private fun generateIv(length: Int): ByteArray { | ||
val b = ByteArray(length) | ||
random.nextBytes(b) | ||
return b | ||
} | ||
|
||
private fun deriveKey(password: String, salt: ByteArray?): SecretKey { | ||
try { | ||
val keySpec = PBEKeySpec(password.toCharArray(), salt!!, ITERATION_COUNT, KEY_LENGTH) | ||
val keyFactory = SecretKeyFactory.getInstance(PBKDF2_DERIVATION_ALGORITHM) | ||
val keyBytes = keyFactory.generateSecret(keySpec).encoded | ||
return SecretKeySpec(keyBytes, "AES") | ||
} catch (e: GeneralSecurityException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
private fun toBase64(bytes: ByteArray): String { | ||
return Base64.encodeToString(bytes, Base64.NO_WRAP) | ||
} | ||
|
||
private fun fromBase64(base64: String): ByteArray { | ||
return Base64.decode(base64, Base64.NO_WRAP) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.rommansabbir.storex | ||
|
||
fun storeXInstance(): StoreX = StoreXCore.instance() | ||
|
||
internal fun Subscriber.getKey(): String { | ||
return "${this.key}_${this.subscriberID}" | ||
} |
13 changes: 13 additions & 0 deletions
13
StoreX/src/main/java/com/rommansabbir/storex/StoreAbleObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.rommansabbir.storex | ||
|
||
import java.io.Serializable | ||
import java.util.* | ||
|
||
open class StoreAbleObject : Serializable { | ||
private val _objectId: String = UUID.randomUUID().toString() | ||
val objectId: String | ||
get() = _objectId | ||
|
||
fun isSameObject(storeAbleObject: StoreAbleObject): Boolean = | ||
storeAbleObject._objectId == _objectId | ||
} |
Oops, something went wrong.