Fix Equality Check in XChaCha20EncryptionResult

The `XChaCha20EncryptionResult` data class currently implements
its own equality check due to the class's byte array members.
However, this is using a standard equality check, `==`, rather
than checking the array's contents. This will cause two results
with the same contained values to return `false` on an equals check.

Confirmed this issue with a unit test, and fixed by changing the
implemented method to use `contentEquals` instead.
This commit is contained in:
Renee Vandervelde 2024-06-14 21:11:29 -05:00
parent 509d22ed05
commit 77445bb5cf
No known key found for this signature in database
GPG Key ID: 1E3B6A09031AACF4
2 changed files with 30 additions and 2 deletions

View File

@ -25,8 +25,8 @@ data class XChaCha20EncryptionResult(val nonce: UByteArray, val encryptionData:
other as XChaCha20EncryptionResult other as XChaCha20EncryptionResult
if (nonce != other.nonce) return false if (!nonce.contentEquals(other.nonce)) return false
if (encryptionData != other.encryptionData) return false if (!encryptionData.contentEquals(other.encryptionData)) return false
return true return true
} }

View File

@ -0,0 +1,28 @@
package com.iospin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.symmetric.XChaCha20EncryptionResult
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
class XChaCha20EncryptionResultTest {
@Test
fun testEquality() {
val firstResult = XChaCha20EncryptionResult(
nonce = ubyteArrayOf(0x12u, 0x34u, 0x56u),
encryptionData = ubyteArrayOf(0x78u, 0x9Au),
)
val secondResult = XChaCha20EncryptionResult(
nonce = ubyteArrayOf(0x12u, 0x34u, 0x56u),
encryptionData = ubyteArrayOf(0x78u, 0x9Au),
)
val differingResult = XChaCha20EncryptionResult(
nonce = ubyteArrayOf(0u),
encryptionData = ubyteArrayOf(0u),
)
assertEquals(firstResult, secondResult)
assertNotEquals(firstResult, differingResult)
assertNotEquals(secondResult, differingResult)
}
}