aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/me/texx/Texx/SecureStorage.kt
blob: 780f418ee1658353edc98abcb3d9c4e9df788f39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package me.texx.Texx

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()
        }
}