diff options
author | Marvin Borner | 2018-09-03 20:00:59 +0200 |
---|---|---|
committer | Marvin Borner | 2018-09-03 20:00:59 +0200 |
commit | 8d524cbbed9f3f1ab98e3e2abc239d23b866980b (patch) | |
tree | f519f9df4562f425bc513cb44cdc5fbd75659274 | |
parent | b1afdcd4ed57c43c3ecda2eaae1671c4a19abcc6 (diff) |
Moved SecureStorage to util :truck:
-rw-r--r-- | app/src/main/java/me/texx/Texx/Util/SecureStorage.kt | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/app/src/main/java/me/texx/Texx/Util/SecureStorage.kt b/app/src/main/java/me/texx/Texx/Util/SecureStorage.kt new file mode 100644 index 0000000..86b5cbf --- /dev/null +++ b/app/src/main/java/me/texx/Texx/Util/SecureStorage.kt @@ -0,0 +1,71 @@ +package me.texx.Texx.Util + +import android.content.Context +import android.preference.PreferenceManager +import android.util.Base64 +import com.kazakago.cryptore.CipherAlgorithm +import com.kazakago.cryptore.Cryptore +import com.madapps.prefrences.EasyPrefrences + +/** + * Class for saving data securely in SharedPreferences + */ +class SecureStorage(private val context: Context) { + /** + * Encrypts and saves the [value] with [key] as index + */ + fun set(key: String, value: String) { + sharedPrefs.putString(key, encryptAES(value)) + } + + /** + * Finds the encrypted value by [key], decrypts it and returns the value as string + */ + fun get(key: String): String? { + return try { + decryptAES(sharedPrefs.getString(key)) + } catch (e: Exception) { + null + } + } + + private val sharedPrefs = EasyPrefrences(context) + + private enum class Alias(val value: String) { + RSA("CIPHER_RSA"), + AES("CIPHER_AES") + } + + private val cryptoreAES: Cryptore by lazy { + val builder = Cryptore.Builder(alias = Alias.AES.value, type = CipherAlgorithm.AES) + builder.build() + } + + private fun encryptAES(plainStr: String): String { + val plainByte = plainStr.toByteArray() + val result = cryptoreAES.encrypt(plainByte = plainByte) + cipherIV = result.cipherIV + return Base64.encodeToString(result.bytes, Base64.DEFAULT) + } + + private fun decryptAES(encryptedStr: String): String { + val encryptedByte = Base64.decode(encryptedStr, Base64.DEFAULT) + val result = cryptoreAES.decrypt(encryptedByte = encryptedByte, cipherIV = cipherIV) + return String(result.bytes) + } + + private var cipherIV: ByteArray? + get() { + val preferences = PreferenceManager.getDefaultSharedPreferences(this.context) + preferences.getString("cipher_iv", null)?.let { + return Base64.decode(it, Base64.DEFAULT) + } + return null + } + set(value) { + val preferences = PreferenceManager.getDefaultSharedPreferences(this.context) + val editor = preferences.edit() + editor.putString("cipher_iv", Base64.encodeToString(value, Base64.DEFAULT)) + editor.apply() + } +}
\ No newline at end of file |