chore: code refactor

This commit is contained in:
rebelonion
2024-02-06 02:16:10 -06:00
parent 8d7b86a667
commit a2e44da99d
334 changed files with 3550 additions and 3092 deletions

View File

@@ -1,8 +1,8 @@
package ani.dantotsu.settings.saving.internal
import android.content.Context
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
class Compat {
companion object {

View File

@@ -8,6 +8,7 @@ data class Pref(
val type: KClass<*>,
val default: Any
)
enum class Location(val location: String, val exportable: Boolean) {
General("ani.dantotsu.general", true),
UI("ani.dantotsu.ui", true),

View File

@@ -2,7 +2,6 @@ package ani.dantotsu.settings.saving.internal
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import java.security.KeyStore
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
@@ -15,7 +14,8 @@ import javax.crypto.spec.PBEKeySpec
class PreferenceKeystore {
companion object {
fun generateKey(alias: String) {
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
val keyGenerator =
KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
keyGenerator.init(
KeyGenParameterSpec.Builder(
@@ -30,17 +30,31 @@ class PreferenceKeystore {
keyGenerator.generateKey()
}
fun encryptWithPassword(password: CharArray, plaintext: String, salt: ByteArray): ByteArray {
fun encryptWithPassword(
password: CharArray,
plaintext: String,
salt: ByteArray
): ByteArray {
val secretKey = deriveKeyFromPassword(password, salt)
val cipher = Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
val cipher =
Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16)))
return cipher.doFinal(plaintext.toByteArray(Charsets.UTF_8))
}
fun decryptWithPassword(password: CharArray, ciphertext: ByteArray, salt: ByteArray): String {
fun decryptWithPassword(
password: CharArray,
ciphertext: ByteArray,
salt: ByteArray
): String {
val secretKey = deriveKeyFromPassword(password, salt)
val cipher = Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16))) // Use the correct IV
val cipher =
Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(
Cipher.DECRYPT_MODE,
secretKey,
IvParameterSpec(ByteArray(16))
) // Use the correct IV
return cipher.doFinal(ciphertext).toString(Charsets.UTF_8)
}

View File

@@ -1,11 +1,7 @@
package ani.dantotsu.settings.saving.internal
import android.content.SharedPreferences
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import ani.dantotsu.connections.discord.serializers.Activity
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.toast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
@@ -28,8 +24,10 @@ class PreferencePackager {
*/
fun unpack(decryptedJson: String): Boolean {
val gson = Gson()
val type = object : TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
val rawPrefsMap: Map<String, Map<String, Map<String, Any>>> = gson.fromJson(decryptedJson, type)
val type = object :
TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
val rawPrefsMap: Map<String, Map<String, Map<String, Any>>> =
gson.fromJson(decryptedJson, type)
val deserializedMap = mutableMapOf<String, Map<String, Any?>>()