diff --git a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt index 87807db..851a5ab 100644 --- a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt +++ b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/ConversionUtil.kt @@ -5,3 +5,12 @@ package com.ionspin.kotlin.crypto.util * ugljesa.jovanovic@ionspin.com * on 22-Jun-2020 */ +@Suppress("CAST_NEVER_SUCCEEDS") +fun ByteArray.asUByteArray() : UByteArray { + return this as UByteArray +} + +@Suppress("CAST_NEVER_SUCCEEDS") +fun UByteArray.asByteArray() : ByteArray { + return this as ByteArray +} diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt index 5e9ac75..e5dbe1f 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -214,8 +214,8 @@ class XChaCha20Poly1305Test { val data = UByteArray(100) { 0U } val result = xcha.encrypt(data) -// assertTrue { -// expected.contentEquals(result) -// } + assertTrue { + expected.contentEquals(result) + } } } diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt index 91e08d9..0834a4a 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -79,17 +79,25 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() { val ciphertext = ByteArray(1 + data.size + 16) sodium.crypto_secretstream_xchacha20poly1305_push( state, ciphertext, null, - data.toByteArray(), data.size.toLong(), - additionalData.toByteArray(), additionalData.size.toLong(), + data.asByteArray(), data.size.toLong(), + additionalData.asByteArray(), additionalData.size.toLong(), 0 ) - return ciphertext.toUByteArray() + return ciphertext.asUByteArray() } actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray { val plaintext = ByteArray(data.size - 17) - TODO() + sodium.crypto_secretstream_xchacha20poly1305_pull( + state, plaintext, null, + data.sliceArray(0 until 1).asByteArray(), + data.sliceArray(1 until data.size).asByteArray(), + (data.size - 17).toLong(), + additionalData.asByteArray(), + additionalData.size.toLong() + ) + return plaintext.asUByteArray() }