🆕 Add Delete function to clear crendentials

This commit is contained in:
Mylloon 2021-08-29 03:15:31 +02:00
parent 82a8ea687e
commit 785d51d70c

View file

@ -1,10 +1,13 @@
package com.mylloon.mobidl package com.mylloon.mobidl
import android.content.Context.MODE_PRIVATE import android.content.Context.MODE_PRIVATE
import android.security.keystore.KeyProperties
import javax.crypto.Cipher
import android.security.keystore.KeyGenParameterSpec import android.security.keystore.KeyGenParameterSpec
import java.security.* import android.security.keystore.KeyProperties
import java.security.KeyPairGenerator
import java.security.KeyStore
import java.security.PrivateKey
import java.security.PublicKey
import javax.crypto.Cipher
class Credentials { class Credentials {
@ -56,7 +59,10 @@ class Credentials {
return decryptedBytes.decodeToString() return decryptedBytes.decodeToString()
} }
fun store(user: String, password: String) { // Store combo encrypted user/pass in shared preferences fun store(
user: String,
password: String
) { // Store combo encrypted user/pass in shared preferences
val userEncrypted = encrypt(user) // encrypt user val userEncrypted = encrypt(user) // encrypt user
val passwordEncrypted = encrypt(password) // encrypt password val passwordEncrypted = encrypt(password) // encrypt password
val context = MainActivity.applicationContext() // get app context val context = MainActivity.applicationContext() // get app context
@ -79,8 +85,12 @@ class Credentials {
val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE)
var data: String? = null var data: String? = null
when (type) { when (type) {
0 -> { data = sharedPref.getString("username", null) } 0 -> {
1 -> { data = sharedPref.getString("password", null) } data = sharedPref.getString("username", null)
}
1 -> {
data = sharedPref.getString("password", null)
}
} }
return if (data != null) { return if (data != null) {
@ -93,4 +103,15 @@ class Credentials {
decrypt(array) decrypt(array)
} else null } else null
} }
fun delete() {
val context = MainActivity.applicationContext() // get app context
// clear shared pref
val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE)
with(sharedPref.edit()) {
this.clear()
this.apply()
}
}
} }