LooKeR c18278517e
refactor: Update encryption to use correct Key and IV
This commit updates the encryption logic to correctly use the secret key and initialization vector (IV) as separate components, rather than combining them into a single `Key` object.

- The `Key` inline class now only holds the secret key bytes.
- Encryption now returns a `Pair` containing the encrypted data (`Encrypted`) and the generated IV (`ByteArray`).
- Decryption requires both the `Key` and the IV as input.
- The `AuthenticationEntity` is updated to store the encrypted password and its corresponding IV separately.
- The encryption test suite is updated to reflect these changes and includes a test for decryption with an incorrect IV.
2025-05-12 12:23:23 +05:30

42 lines
1.5 KiB
Kotlin

package com.looker.droidify.encryption
import com.looker.droidify.data.encryption.Key
import com.looker.droidify.data.encryption.generateSecretKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertNotEquals
class EncryptionTest {
private val secretKey = Key(generateSecretKey())
private val fakeKey = Key(generateSecretKey())
private val testString = "This is a test string"
@Test
fun `encrypt and decrypt`() {
val (encrypted, iv) = secretKey.encrypt(testString)
assertNotEquals(testString, encrypted.value, "Encrypted and original string are the same")
val decrypted = encrypted.decrypt(secretKey, iv)
assertEquals(testString, decrypted, "Decrypted string does not match original")
}
@Test
fun `encrypt and decrypt with fake key`() {
val (encrypted, iv) = secretKey.encrypt(testString)
assertNotEquals(testString, encrypted.value, "Encrypted and original string are the same")
assertFails { encrypted.decrypt(fakeKey, iv) }
}
@Test
fun `encrypt and decrypt with wrong iv`() {
val (encrypted, iv) = secretKey.encrypt(testString)
assertNotEquals(testString, encrypted.value, "Encrypted and original string are the same")
val fakeIv = iv.clone().apply { this[lastIndex] = "1".toByte() }
val output = encrypted.decrypt(secretKey, fakeIv)
assertNotEquals(testString, output, "Encrypted and original string are the same")
}
}