Skip to content

Commit fb9e6f8

Browse files
committed
- Write section completed
- Working on the Read Section
1 parent 35fa90f commit fb9e6f8

File tree

11 files changed

+154
-31
lines changed

11 files changed

+154
-31
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
implementation project(path: ':library')
3030
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3131
implementation 'androidx.core:core-ktx:1.3.1'
32-
implementation 'androidx.appcompat:appcompat:1.1.0'
32+
implementation 'androidx.appcompat:appcompat:1.2.0'
3333
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3434

3535
testImplementation 'junit:junit:4.13'

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
33
ext.kotlin_version = "1.3.72"
4+
ext.kotlin_coroutines_version = "1.3.4"
45
repositories {
56
google()
67
jcenter()

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ android {
2727
dependencies {
2828
implementation fileTree(dir: "libs", include: ["*.jar"])
2929
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
3031
}

library/src/main/java/io/easyprefs/contract/Edit.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ interface Edit {
66

77
fun remove(key: String): Boolean
88
fun removeAsync(key: String)
9+
10+
fun hasKey(key: String): Boolean
911
}

library/src/main/java/io/easyprefs/impl/EasyPrefImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ object EasyPrefImpl : EasyPref {
5353
context.getSharedPreferences(
5454
getFileName(context),
5555
Context.MODE_PRIVATE
56-
)
56+
), encType, aesKey
5757
)
5858
}
5959

@@ -67,7 +67,7 @@ object EasyPrefImpl : EasyPref {
6767
context.getSharedPreferences(
6868
getFileNameOn(fileName),
6969
Context.MODE_PRIVATE
70-
)
70+
), encType, aesKey
7171
)
7272
}
7373

@@ -82,7 +82,7 @@ object EasyPrefImpl : EasyPref {
8282
context.getSharedPreferences(
8383
getFileNameOn(fileName),
8484
mode
85-
)
85+
), encType, aesKey
8686
)
8787
}
8888

@@ -91,7 +91,7 @@ object EasyPrefImpl : EasyPref {
9191
context.getSharedPreferences(
9292
getFileName(context),
9393
Context.MODE_PRIVATE
94-
).edit()
94+
)
9595
)
9696
}
9797

@@ -105,7 +105,7 @@ object EasyPrefImpl : EasyPref {
105105
context.getSharedPreferences(
106106
getFileNameOn(fileName),
107107
Context.MODE_PRIVATE
108-
).edit()
108+
)
109109
)
110110
}
111111

@@ -120,7 +120,7 @@ object EasyPrefImpl : EasyPref {
120120
context.getSharedPreferences(
121121
getFileNameOn(fileName),
122122
mode
123-
).edit()
123+
)
124124
)
125125
}
126126

library/src/main/java/io/easyprefs/impl/EditImpl.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ package io.easyprefs.impl
33
import android.content.SharedPreferences
44
import io.easyprefs.contract.Edit
55

6-
class EditImpl(private val edit: SharedPreferences.Editor) : Edit {
6+
class EditImpl(private val pref: SharedPreferences) : Edit {
77

88
override fun clear(): Boolean {
9-
return edit.clear().commit()
9+
return pref.edit().clear().commit()
1010
}
1111

1212
override fun clearAsync() {
13-
edit.clear().apply()
13+
pref.edit().clear().apply()
1414
}
1515

1616
override fun remove(key: String): Boolean {
17-
return edit.remove(key).commit()
17+
return pref.edit().remove(key).commit()
1818
}
1919

2020
override fun removeAsync(key: String) {
21-
edit.remove(key).apply()
21+
pref.edit().remove(key).apply()
22+
}
23+
24+
override fun hasKey(key: String): Boolean {
25+
return pref.all.keys.contains(key)
2226
}
2327
}

library/src/main/java/io/easyprefs/impl/ReadImpl.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,36 @@ package io.easyprefs.impl
22

33
import android.content.SharedPreferences
44
import io.easyprefs.contract.Read
5+
import io.easyprefs.secure.Crypt
6+
import io.easyprefs.typedef.Encryption
57

6-
class ReadImpl(private val pref: SharedPreferences) : Read {
8+
class ReadImpl(
9+
private val pref: SharedPreferences,
10+
private val encType: Encryption,
11+
private val aesKey: String
12+
) : Read {
713

814
override fun int(key: String, defaultValue: Int): Int {
9-
return pref.getInt(key, defaultValue)
15+
return if (encType == Encryption.NONE) {
16+
pref.getInt(key, defaultValue)
17+
} else {
18+
decrypt(key, defaultValue.toString()).toInt()
19+
}
20+
}
21+
22+
private fun decrypt(key: String, defaultValue: String): String {
23+
var value = pref.getString(key, defaultValue) ?: defaultValue
24+
return if (aesKey.isEmpty()) {
25+
if (value != defaultValue) {
26+
value = Crypt.decrypt(key, value)
27+
}
28+
value
29+
} else {
30+
if (value != defaultValue) {
31+
value = Crypt.decrypt(aesKey, value)
32+
}
33+
value
34+
}
1035
}
1136

1237
override fun string(key: String, defaultValue: String): String {

library/src/main/java/io/easyprefs/impl/WriteImpl.kt

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package io.easyprefs.impl
22

33
import android.content.SharedPreferences
44
import io.easyprefs.contract.Write
5+
import io.easyprefs.secure.Crypt
56
import io.easyprefs.typedef.Encryption
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.launch
9+
import org.json.JSONArray
610

711
class WriteImpl(
812
private val edit: SharedPreferences.Editor,
@@ -14,63 +18,129 @@ class WriteImpl(
1418
return if (encType == Encryption.NONE) {
1519
edit.putInt(key, value).commit()
1620
} else {
21+
crypt(key, value.toString())
22+
}
23+
}
24+
25+
private fun crypt(key: String, value: String): Boolean {
26+
return if (aesKey.isEmpty()) {
27+
edit.putString(key, Crypt.encrypt(key, value)).commit()
28+
} else {
29+
edit.putString(key, Crypt.encrypt(aesKey, value)).commit()
30+
}
31+
}
32+
33+
private fun cryptAsync(key: String, value: String) {
34+
GlobalScope.launch {
1735
if (aesKey.isEmpty()) {
18-
edit.putInt(key, value).commit()
36+
edit.putString(key, Crypt.encrypt(key, value)).apply()
1937
} else {
20-
edit.putInt(key, value).commit()
38+
edit.putString(key, Crypt.encrypt(aesKey, value)).apply()
2139
}
2240
}
2341
}
2442

2543
override fun intAsync(key: String, value: Int) {
26-
edit.putInt(key, value).apply()
44+
if (encType == Encryption.NONE) {
45+
edit.putInt(key, value).apply()
46+
} else {
47+
cryptAsync(key, value.toString())
48+
}
2749
}
2850

2951
override fun string(key: String, value: String): Boolean {
30-
return edit.putString(key, value).commit()
52+
return if (encType == Encryption.NONE) {
53+
edit.putString(key, value).commit()
54+
} else {
55+
crypt(key, value)
56+
}
3157
}
3258

3359
override fun stringAsync(key: String, value: String) {
34-
edit.putString(key, value).apply()
60+
if (encType == Encryption.NONE) {
61+
edit.putString(key, value).apply()
62+
} else {
63+
cryptAsync(key, value)
64+
}
3565
}
3666

3767
override fun long(key: String, value: Long): Boolean {
38-
return edit.putLong(key, value).commit()
68+
return if (encType == Encryption.NONE) {
69+
edit.putLong(key, value).commit()
70+
} else {
71+
crypt(key, value.toString())
72+
}
3973
}
4074

4175
override fun longAsync(key: String, value: Long) {
42-
edit.putLong(key, value).apply()
76+
if (encType == Encryption.NONE) {
77+
edit.putLong(key, value).apply()
78+
} else {
79+
cryptAsync(key, value.toString())
80+
}
4381
}
4482

4583
override fun float(key: String, value: Float): Boolean {
46-
return edit.putFloat(key, value).commit()
84+
return if (encType == Encryption.NONE) {
85+
edit.putFloat(key, value).commit()
86+
} else {
87+
crypt(key, value.toString())
88+
}
4789
}
4890

4991
override fun floatAsync(key: String, value: Float) {
50-
edit.putFloat(key, value).apply()
92+
if (encType == Encryption.NONE) {
93+
edit.putFloat(key, value).apply()
94+
} else {
95+
cryptAsync(key, value.toString())
96+
}
5197
}
5298

5399
override fun double(key: String, value: Double): Boolean {
54-
return string(key, value.toString())
100+
return if (encType == Encryption.NONE) {
101+
string(key, value.toString())
102+
} else {
103+
crypt(key, value.toString())
104+
}
55105
}
56106

57107
override fun doubleAsync(key: String, value: Double) {
58-
stringAsync(key, value.toString())
108+
if (encType == Encryption.NONE) {
109+
stringAsync(key, value.toString())
110+
} else {
111+
cryptAsync(key, value.toString())
112+
}
59113
}
60114

61115
override fun boolean(key: String, value: Boolean): Boolean {
62-
return edit.putBoolean(key, value).commit()
116+
return if (encType == Encryption.NONE) {
117+
edit.putBoolean(key, value).commit()
118+
} else {
119+
crypt(key, value.toString())
120+
}
63121
}
64122

65123
override fun booleanAsync(key: String, value: Boolean) {
66-
edit.putBoolean(key, value).apply()
124+
if (encType == Encryption.NONE) {
125+
edit.putBoolean(key, value).apply()
126+
} else {
127+
cryptAsync(key, value.toString())
128+
}
67129
}
68130

69131
override fun stringSet(key: String, value: Set<String>): Boolean {
70-
return edit.putStringSet(key, value).commit()
132+
return if (encType == Encryption.NONE) {
133+
edit.putStringSet(key, value).commit()
134+
} else {
135+
crypt(key, JSONArray(value).toString())
136+
}
71137
}
72138

73139
override fun stringSetAsync(key: String, value: Set<String>) {
74-
edit.putStringSet(key, value).apply()
140+
if (encType == Encryption.NONE) {
141+
edit.putStringSet(key, value).apply()
142+
} else {
143+
cryptAsync(key, JSONArray(value).toString())
144+
}
75145
}
76-
}
146+
}

library/src/main/java/io/easyprefs/secure/AESCrypt.kt renamed to library/src/main/java/io/easyprefs/secure/AesCrypt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import javax.crypto.spec.SecretKeySpec
1313
* Encrypt and decrypt messages using AES 256 bit encryption that are compatible with AESCrypt-ObjC and AESCrypt Ruby.
1414
*/
1515
@Suppress("unused")
16-
object AESCrypt {
16+
object AesCrypt {
1717

1818
//AESCrypt-ObjC uses CBC and PKCS7Padding
1919
private const val AES_MODE = "AES/CBC/PKCS7Padding"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.easyprefs.secure
2+
3+
import io.easyprefs.secure.contract.CryptProvider
4+
5+
object Crypt : CryptProvider {
6+
7+
override fun encrypt(key: String, value: String): String {
8+
return AesCrypt.encrypt(key, value)
9+
}
10+
11+
override fun decrypt(key: String, value: String): String {
12+
return AesCrypt.decrypt(key, value)
13+
}
14+
}

0 commit comments

Comments
 (0)