Experiment with asByteArray()

This commit is contained in:
Ugljesa Jovanovic 2020-07-07 21:19:20 +02:00 committed by Ugljesa Jovanovic
parent 8fb4f91374
commit 9751f80347
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
3 changed files with 24 additions and 7 deletions

View File

@ -5,3 +5,12 @@ package com.ionspin.kotlin.crypto.util
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020 * 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
}

View File

@ -214,8 +214,8 @@ class XChaCha20Poly1305Test {
val data = UByteArray(100) { 0U } val data = UByteArray(100) { 0U }
val result = xcha.encrypt(data) val result = xcha.encrypt(data)
// assertTrue { assertTrue {
// expected.contentEquals(result) expected.contentEquals(result)
// } }
} }
} }

View File

@ -79,17 +79,25 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val ciphertext = ByteArray(1 + data.size + 16) val ciphertext = ByteArray(1 + data.size + 16)
sodium.crypto_secretstream_xchacha20poly1305_push( sodium.crypto_secretstream_xchacha20poly1305_push(
state, ciphertext, null, state, ciphertext, null,
data.toByteArray(), data.size.toLong(), data.asByteArray(), data.size.toLong(),
additionalData.toByteArray(), additionalData.size.toLong(), additionalData.asByteArray(), additionalData.size.toLong(),
0 0
) )
return ciphertext.toUByteArray() return ciphertext.asUByteArray()
} }
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray { actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
val plaintext = ByteArray(data.size - 17) 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()
} }