Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/a1573595/SecuritySharedPreferences

A small SharedPreferences wrapper and cryptographic android library power by Kotlin.
https://github.com/a1573595/SecuritySharedPreferences

aes android base64 keystore kotlin rsa security sharedpreferences

Last synced: 3 months ago
JSON representation

A small SharedPreferences wrapper and cryptographic android library power by Kotlin.

Lists

README

        

*Read this in other languages: [English](README.md), [δΈ­ζ–‡](README.zh-tw.md).*

# SecuritySharedPreferences
A small SharedPreferences wrapper and cryptographic android library power by Kotlin.

⚑ Kotlin powered.

πŸš€ Easy to use.

πŸ”’ Protect data (Android KeyStore + AES256 SIV / + AES256 GCM).

πŸ§ͺ Support Unit test.

## Difference
Difference between the android SharedPreference and SecuritySharedPreferences library.
* SharedPreference
```

[email protected]

```

* SecuritySharedPreferences
```

AVQneV9aILgdLAT+OYJowJYWzeRktEj7gsttnTN4bLXMa690QKYnBWq1MuwnFpYAhjV/Gna2axuvqw==

```

## Supported Android Versions
- Android 5.1 Lollipop(API level 21) or higher.

## Gradle
```groovy
allprojects {
repositories {
...

maven { url 'https://jitpack.io' }
}
}
```

```groovy
dependencies {
implementation 'com.github.a1573595:SecuritySharedPreferences:2.0.3'

// Optional - Unit test
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'androidx.test:core-ktx:1.4.0'
testImplementation 'androidx.test.ext:junit-ktx:1.1.3'
testImplementation 'org.robolectric:robolectric:4.6.1'
}
```

## Usage
Define your SecuritySharedPreferences and add the parameters to be stored.
```kotlin
class DefaultPreferences(context: Context) :
SecuritySharedPreferences(context, context.packageName) {
// Add your stored parameter...
var userName by PreferencesData("Name", "Chien")

var email by PreferencesData("Email", "")

var address by PreferencesData("Address", "")

var age by PreferencesData("Age", 25)

var height by PreferencesData("Height", 1.75f)
}
```

Or you can customize the KeyStore alias, SharedPreferences name and mode.
```kotlin
class CustomPreferences(context: Context) :
SecuritySharedPreferences(context, "${context.packageName}.custom") {
// Add your stored parameter...
}
```

Initialization SecuritySharedPreferences instance, easy to get and set stored value.
```kotlin
val preferencesManager = DefaultPreferences(this)

// get value from SecuritySharedPreferences
val email = preferencesManager.email

// save value into SecuritySharedPreferences
preferencesManager.email = "[email protected]"
```

## Unit test
```kotlin
@RunWith(AndroidJUnit4::class)
class SecuritySharedPreferencesTest {
companion object {
@JvmStatic
@BeforeClass
fun beforeClass() {
FakeAndroidKeyStore.setup
}
}

private val preferences: CustomPreferences =
CustomPreferences(ApplicationProvider.getApplicationContext())

...
}
```

## Reference
[AndroidKeyStore](https://github.com/joetsaitw/AndroidKeyStore)

[Testing Jetpack Security with Robolectric](https://proandroiddev.com/testing-jetpack-security-with-robolectric-9f9cf2aa4f61)